Java 8 Functional Interface

Java 8 新特性Java 8 New Features


A functional interface (Functional Interface) is an interface that has exactly one abstract method, but can have multiple non-abstract methods.

Functional interfaces can be implicitly converted to lambda expressions.

Lambda expressions and method references (which can actually also be considered lambda expressions).

For example, define a functional interface as follows:

@FunctionalInterface
interface GreetingService 
{
    void sayMessage(String message);
}

Then you can use a lambda expression to represent an implementation of the interface (Note: before Java 8, it was usually implemented with anonymous classes):

GreetingService greetService1 = message -> System.out.println("Hello " + message);

Functional interfaces can support lambda in a friendly way for 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

Functional interfaces newly added in JDK 1.8:

  • java.util.function

java.util.function contains many classes to support functional programming in Java. The functional interfaces in this package include:

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 on two operands of the same type, and returns a result of the same type as the operands.

4 BiPredicate<T,U>

Represents a boolean-valued method with two parameters.

5 BooleanSupplier

Represents a provider of a boolean-valued result.

6 Consumer<T>

Represents an operation that accepts one input parameter and returns no result.

7 DoubleBinaryOperator

Represents an operation on two double-valued operands, and returns a double-valued result.

8 DoubleConsumer

Represents an operation that accepts a double-valued parameter and returns no result.

9 DoubleFunction<R>

Represents a method that accepts a double-valued parameter and returns a result.

10 DoublePredicate

Represents a boolean-valued method with a double-valued parameter.

11 DoubleSupplier

Represents a provider of a double-valued result.

12 DoubleToIntFunction

Accepts a double type input and returns an int type result.

13 DoubleToLongFunction

Accepts a double type input and returns a long type result.

14 DoubleUnaryOperator

Accepts a parameter of type double, and the return value type is also double.

15 Function<T,R>

Accepts an input parameter and returns a result.

16 IntBinaryOperator

Accepts two parameters both of type int, and the return value type is also int.

17 IntConsumer

Accepts an int type input parameter, no return value.

18 IntFunction<R>

Accepts an int type input parameter and returns a result.

19 IntPredicate

Accepts an int input parameter and returns a boolean result.

20 IntSupplier

No parameters, returns an int type result.

21 IntToDoubleFunction

Accepts an int type input and returns a double type result.

22 IntToLongFunction

Accepts an int type input and returns a long type result.

23 IntUnaryOperator

Accepts a parameter of type int, and the return value type is also int.

24 LongBinaryOperator

Accepts two parameters both of type long, and the return value type is also long.

25 LongConsumer

Accepts a long type 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 type result.

28 LongSupplier

No parameters, returns a value 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

Accepts a parameter of type long, and the return value type is also long.

32 ObjDoubleConsumer<T>

Accepts an Object type and a double type input parameter, no return value.

33 ObjIntConsumer<T>

Accepts an Object type and an int type input parameter, no return value.

34 ObjLongConsumer<T>

Accepts an Object type and a long type input parameter, no return value.

35 Predicate<T>

Accepts an input parameter and returns a boolean result.

36 Supplier<T>

No parameters, returns a result.

37 ToDoubleBiFunction<T,U>

Accepts two input parameters and returns a double type result.

38 ToDoubleFunction<T>

Accepts an input parameter and returns a double type result.

39 ToIntBiFunction<T,U>

Accepts two input parameters and returns an int type result.

40 ToIntFunction<T>

Accepts an 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>

Accepts an input parameter and returns a long type result.

43 UnaryOperator<T>

Accepts a parameter of type T, and the return value type is also T.


Functional Interface Example

The Predicate<T> interface is a functional interface that accepts an input parameter T and returns a boolean result.

This interface contains various default methods to combine Predicates into other complex logic (such as: AND, OR, NOT).

This interface is used to test whether an object is true or false.

We can learn about the usage of the functional interface Predicate<T> through the following example (Java8Tester.java):

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. //If n exists, the test method returns true. System.out.println("Print all data:"); //Pass 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("Print 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("Print 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, and the output result is:

$ javac Java8Tester.java 
$ java Java8Tester
输出所有数据:
1 
2 
3 
4 
5 
6 
7 
8 
9 
输出所有偶数:
2 
4 
6 
8 
输出大于 3 的所有数字:
4 
5 
6 
7 
8 
9 

Java 8 新特性Java 8 New Features

Other extensions