YouTip LogoYouTip

Java8 Functional Interfaces

[![Image 1: Java 8 New Features](#) Java 8 New Features](#) * * * A Functional Interface is an interface that has exactly one abstract method, but can have multiple non-abstract methods. A functional interface can be implicitly converted to a lambda expression. Lambda expressions and method references (which can also be considered lambda expressions) are based on this concept. For example, if we define a functional interface as follows: ```java @FunctionalInterface interface GreetingService { void sayMessage(String message); } Then we can use a lambda expression to represent an implementation of this interface (Note: Before Java 8, this was typically done using an anonymous class): ```java GreetingService greetService1 = message -> System.out.println("Hello " + message); Functional interfaces provide friendly support for lambdas on existing functions. Existing functional interfaces in 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 New functional interfaces added in JDK 1.8: * java.util.function `java.util.function` contains many classes to support Java's functional programming. The functional interfaces in this package include: | No. | Interface & Description | | --- | --- | | 1 | **BiConsumer** Represents an operation that accepts two input arguments and returns no result. | | 2 | **BiFunction** Represents a function that accepts two arguments and produces a result. | | 3 | **BinaryOperator** Represents an operation upon two operands of the same type, producing a result of the same type as the operands. | | 4 | **BiPredicate** Represents a predicate (boolean-valued function) of two arguments. | | 5 | **BooleanSupplier** Represents a supplier of boolean-valued results. | | 6 | **Consumer** Represents an operation that accepts a single input argument and returns no result. | | 7 | **DoubleBinaryOperator** Represents an operation upon two double-valued operands and returning a double-valued result. | | 8 | **DoubleConsumer** Represents an operation that accepts a double-valued argument and returns no result. | | 9 | **DoubleFunction** Represents a function that accepts a double-valued argument and produces a result. | | 10 | **DoublePredicate** Represents a predicate (boolean-valued function) of one double-valued argument. | | 11 | **DoubleSupplier** Represents a supplier of double-valued results. | | 12 | **DoubleToIntFunction** Represents a function that accepts a double-valued argument and produces an int-valued result. | | 13 | **DoubleToLongFunction** Represents a function that accepts a double-valued argument and produces a long-valued result. | | 14 | **DoubleUnaryOperator** Represents an operation on a single double-valued operand that produces a double-valued result. | | 15 | **Function** Represents a function that accepts one argument and produces a result. | | 16 | **IntBinaryOperator** Represents an operation upon two int-valued operands and returning an int-valued result. | | 17 | **IntConsumer** Represents an operation that accepts a single int-valued argument and returns no result. | | 18 | **IntFunction** Represents a function that accepts an int-valued argument and produces a result. | | 19 | **IntPredicate** Represents a predicate (boolean-valued function) of one int-valued argument. | | 20 | **IntSupplier** Represents a supplier of int-valued results. | | 21 | **IntToDoubleFunction** Represents a function that accepts an int-valued argument and produces a double-valued result. | | 22 | **IntToLongFunction** Represents a function that accepts an int-valued argument and produces a long-valued result. | | 23 | **IntUnaryOperator** Represents an operation on a single int-valued operand that produces an int-valued result. | | 24 | **LongBinaryOperator** Represents an operation upon two long-valued operands and returning a long-valued result. | | 25 | **LongConsumer** Represents an operation that accepts a single long-valued argument and returns no result. | | 26 | **LongFunction** Represents a function that accepts a long-valued argument and produces a result. | | 27 | **LongPredicate** Represents a predicate (boolean-valued function) of one long-valued argument. | | 28 | **LongSupplier** Represents a supplier of long-valued results. | | 29 | **LongToDoubleFunction** Represents a function that accepts a long-valued argument and produces a double-valued result. | | 30 | **LongToIntFunction** Represents a function that accepts a long-valued argument and produces an int-valued result. | | 31 | **LongUnaryOperator** Represents an operation on a single long-valued operand that produces a long-valued result. | | 32 | **ObjDoubleConsumer** Represents an operation that accepts an object-valued and a double-valued argument, and returns no result. | | 33 | **ObjIntConsumer** Represents an operation that accepts an object-valued and an int-valued argument, and returns no result. | | 34 | **ObjLongConsumer** Represents an operation that accepts an object-valued and a long-valued argument, and returns no result. | | 35 | **Predicate** Represents a predicate (boolean-valued function) of one argument. | | 36 | **Supplier** Represents a supplier of results. | | 37 | **ToDoubleBiFunction** Represents a function that accepts two arguments and produces a double-valued result. | | 38 | **ToDoubleFunction** Represents a function that accepts an argument and produces a double-valued result. | | 39 | **ToIntBiFunction** Represents a function that accepts two arguments and produces an int-valued result. | | 40 | **ToIntFunction** Represents a function that accepts an argument and produces an int-valued result. | | 41 | **ToLongBiFunction** Represents a function that accepts two arguments and produces a long-valued result. | | 42 | **ToLongFunction** Represents a function that accepts an argument and produces a long-valued result. | | 43 | **UnaryOperator** Represents an operation on a single operand that produces a result of the same type as its operand. | * * * ## Functional Interface Example The `Predicate` interface is a functional interface that accepts an input argument of type `T` and returns a boolean result. This interface contains various default methods to combine `Predicate` into other complex logic (such as: AND, OR, NOT). This interface is used to test whether an object is true or false. We can understand the use of the functional interface `Predicate` through the following example (`Java8Tester.java`): ## Java8Tester.java File ```java import java.util.Arrays; import java.util.List; import java.util.function.Predicate; public class Java8Tester { public static void main(String args[]) { List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); System.out.println("Output all data:"); eval(list, n -> true); System.out.println("Output all even numbers:"); eval(list, n -> n % 2 == 0); System.out.println("Output all numbers greater than 3:"); eval(list, n -> n > 3); } public static void eval(List list, Predicate predicate) { for (Integer n : list) { if (predicate.test(n)) { System.out.println(n + " "); } } } } Executing the above script, the output result is: $ javac Java8Tester.java $ java Java8Tester Output all data: 1 2 3 4 5 6 7 8 9 Output all even numbers: 2 4 6 8 Output all numbers greater than 3: 4 5 6 7 8 9 * * Java 8 New Features](#)
← Java Object WaitJava Object Notify β†’