Functional Interface is an interface that has one and only one abstract method, but can have multiple non-abstract methods.
Functional interfaces can be implicitly converted to lambda expressions.
Functional interfaces can support lambdas with existing functions.
Functional interfaces that existed before JDK 1.8:
- java.lang.Runnable
- java.util.concurrent.Callable
- java.security.PrivilegedAction
- java.util.Comparator
- java.io.FileFilter
- java.nio.file.PathMatcher
- java.lang.reflect.InvocationHandler
- java.beans.PropertyChangeListener
- java.awt.event.ActionListener
- javax.swing.event.ChangeListener
JDK 1.8 new function interface:
- java.util.function
Java.util.function It contains many classes to support Java's functional programming. The functional interfaces in this package are:
| No. | Interface & Description |
|---|---|
| 1 | BiConsumer<T,U>
Represents an operation that accepts two input parameters and returns no result
|
| 2 | BiFunction<T,U,R>
Represents a method that accepts two input parameters and returns a result
|
| 3 | BinaryOperator<T>
Represents an operation that operates on two operators of the same type and returns the result of the operator of the same type
|
| 4 | BiPredicate<T,U>
A boolean value method that represents a two arguments
|
| 5 | BooleanSupplier
Represents the provider of the boolean result
|
| 6 | Consumer<T>
Represents an operation that accepts one input parameter and no return
|
| 7 | DoubleBinaryOperator
Represents an operation that operates on two double value operators and returns the result of a double value.
|
| 8 | DoubleConsumer
Represents an operation that accepts a double value parameter and does not return a result.
|
| 9 | DoubleFunction<R>
Represents a method that takes a double value parameter and returns the result
|
| 10 | DoublePredicate
Represents a boolean value method with a double value parameter
|
| 11 | DoubleSupplier
Represents a provider of a double value structure
|
| 12 | DoubleToIntFunction
Accepts a double type input and returns an int type result.
|
| 13 | DoubleToLongFunction
Accept a double type input, return a long type result
|
| 14 | DoubleUnaryOperator
Accept a parameter of the same type double, the return value type is double.
|
| 15 | Function<T,R>
Accept one input parameter and return one result.
|
| 16 | IntBinaryOperator
Accept two parameters are the same type int, return type is also int.
|
| 17 | IntConsumer
Accepts an input parameter of type int, with no return value.
|
| 18 | IntFunction<R>
Accepts an input parameter of type int and returns a result.
|
| 19 | IntPredicate
: Accepts an int input parameter and returns a Boolean result.
|
| 20 | IntSupplier
Without parameters, returns an int type result.
|
| 21 | IntToDoubleFunction
Accepts an int type input and returns a double type result.
|
| 22 | IntToLongFunction
Accepts an input of type int and returns a result of type long.
|
| 23 | IntUnaryOperator
Accept an argument of the same type int, and the return type is also int.
|
| 24 | LongBinaryOperator
Accept two parameters are the same type long, the return value type is also long.
|
| 25 | LongConsumer
Accepts a long type of input parameter, no return value.
|
| 26 | LongFunction<R>
Accepts a long type input parameter and returns a result.
|
| 27 | LongPredicate
R accepts a long input parameter and returns a boolean result.
|
| 28 | LongSupplier
Without arguments, returns a result of type long.
|
| 29 | LongToDoubleFunction
Accepts a long type input and returns a double type result.
|
| 30 | LongToIntFunction
Accepts a long type input and returns an int type result.
|
| 31 | LongUnaryOperator
Accept a parameter of the same type long, the return value type is also long.
|
| 32 | ObjDoubleConsumer<T>
Accepts an object type and a double input parameter, no return value.
|
| 33 | ObjIntConsumer<T>
Accepts an input parameter of type object and an int, with no return value.
|
| 34 | ObjLongConsumer<T>
Accepts an object type and a long type of input parameter, no return value.
|
| 35 | Predicate<T>
Accepts one input parameter and returns a Boolean result.
|
| 36 | Supplier<T>
No parameters, return a result.
|
| 37 | ToDoubleBiFunction<T,U>
Accepts two input parameters and returns a double type result
|
| 38 | ToDoubleFunction<T>
Accept one input parameter and return a double type result
|
| 39 | ToIntBiFunction<T,U>
Accepts two input parameters and returns an int type result.
|
| 40 | ToIntFunction<T>
Accepts one input parameter and returns an int type result.
|
| 41 | ToLongBiFunction<T,U>
Accepts two input parameters and returns a long type result.
|
| 42 | ToLongFunction<T>
Accept one input parameter and return a long type result.
|
| 43 | UnaryOperator<T>
Accept one argument of type T and return value type is also T.
|
Functional interface instance
The Predicate <T> interface is a functional interface that takes an input parameter T and returns a Boolean result.
The interface contains a variety of default methods to combine Predicate into other complex logic (eg, AND, OR, NOT).
This interface is used to test whether the object is true or false.
We can use the following example (Java8Tester.java) to understand the use of the functional interface Predicate <T>:
Java8Tester.java file
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Java8Tester {
public static void main(String args[]){
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
// Predicate<Integer> predicate = n -> true
// n is a parameter passed to the test method of the Predicate interface
// n test method returns true if it exists
System . out . println ( " Output all data: " ) ;
// Pass the parameter n
eval(list, n->true);
// Predicate<Integer> predicate1 = n -> n%2 == 0
// n is a parameter passed to the test method of the Predicate interface
// if n%2 is 0 the test method returns true
System.out.println("Output all even numbers:");
eval(list, n-> n%2 == 0 );
// Predicate<Integer> predicate2 = n -> n > 3
// n is a parameter passed to the test method of the Predicate interface
// If n is greater than 3 the test method returns true
System.out.println("Output all numbers greater than 3:");
eval(list, n-> n > 3 );
}
public static void eval(List<Integer> list, Predicate<Integer> predicate) {
for(Integer n: list) {
if(predicate.test(n)) {
System.out.println(n + " ");
}
}
}
}
Execute the above script, the output is:
$ javac Java8Tester.java $ java Java8Tester outputs all data: 1 2 3 4 5 6 7 8 9 Outputs all even numbers: 2 4 6 8 Outputs all numbers greater than 3: 4 5 6 7 8 9
Comments
Post a Comment