English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/)

Java Reader/Writer

Java other topics

Java 8 Functional interface

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 also be considered as Lambda expressions).

As defined by a functional interface as follows:

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

Then you can use Lambda expressions to represent an implementation of the interface (note: JAVA 8 Previously, it was usually implemented using anonymous classes):

GreetingService greetService1 Equal message -System.out.println("Hello "}} + message);

Functional interfaces can support lambda in an existing function in a friendly manner.

JDK 1.8 Existing functional interfaces before:

  • 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 Newly added functional interfaces:

  • java.util.function

java.util.function It contains many classes to support Java's functional programming, the functional interfaces in this package include:

Serial NumberInterface & Description
1BiConsumer<T,U>

Represents an operation that accepts two input parameters and does not return anything

2BiFunction<T,U,R>

Represents a method that accepts two input parameters and returns a result

3BinaryOperator<T>

Represents an operation that acts on two operands of the same type and returns a result of the same type

4BiPredicate<T,U>

Represents a boolean value method with two parameters

5BooleanSupplier

Represents a supplier of a boolean value result

6Consumer<T>

Represents an operation that accepts an input parameter and does not return anything

7DoubleBinaryOperator

Represents an operation that acts on two double value operands and returns a double value result.

8DoubleConsumer

Represents an operation that accepts a double value parameter and does not return a result.

9DoubleFunction<R>

Represents a method that accepts a double value parameter and returns a result

10DoublePredicate

Represents a boolean value method with a double value parameter

11DoubleSupplier

Represents a supplier of a double value structure

12DoubleToIntFunction

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

13DoubleToLongFunction

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

14DoubleUnaryOperator

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

15Function<T,R>

Accepts an input parameter and returns a result.

16IntBinaryOperator

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

17IntConsumer

Accepts an int type input parameter, with no return value.

18IntFunction<R>

Accepts an int type input parameter and returns a result.

19IntPredicate

Accepts an int input parameter and returns a boolean type result.

20IntSupplier

No parameters, returns an int type result.

21IntToDoubleFunction

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

22IntToLongFunction

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

23IntUnaryOperator

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

24LongBinaryOperator

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

25LongConsumer

Accepts a long type input parameter, with no return value.

26LongFunction<R>

Accepts a long type input parameter and returns a result.

27LongPredicate

R accepts a long input parameter and returns a boolean type result.

28LongSupplier

No parameters, returns a result of type long.

29LongToDoubleFunction

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

30LongToIntFunction

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

31LongUnaryOperator

Accepts a parameter of type long and returns a long type value.

32ObjDoubleConsumer<T>

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

33ObjIntConsumer<T>

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

34ObjLongConsumer<T>

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

35Predicate<T>

Accepts an input parameter and returns a boolean result.

36Supplier<T>

No parameters, returns a result.

37ToDoubleBiFunction<T,U>

Accepts two input parameters and returns a double type result

38ToDoubleFunction<T>

Accepts an input parameter and returns a double type result

39ToIntBiFunction<T,U>

Accepts two input parameters and returns an int type result.

40ToIntFunction<T>

Accepts an input parameter and returns an int type result.

41ToLongBiFunction<T,U>

Accepts two input parameters and returns a long type result.

42ToLongFunction<T>

Accepts an input parameter and returns a long type result.

43UnaryOperator<T>

Accepts a parameter of type T, and the return 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 includes 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 use the following examples (Java8To learn about the usage of the functional interface Predicate <T> in Tester.java):

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("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 For 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 test method returns true
        
      System.out.println("Output Numbers Greater Than 3 All Numbers: ");
      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 will be:

$ 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 Numbers Greater Than 3 All Numbers:
4 
5 
6 
7 
8 
9

Java 8 New Features