New Features of Java 8-Functional Interface

New Features of Java 8-Functional Interface

JDK1.8 provides a special interface-Functional Interface. Compared with ordinary interfaces, it has one more method than ordinary interfaces. Functional interfaces can be implicitly converted to lambda expressions. The existing functions of the functional interface can support lambda friendly.

In fact, there were some functional interfaces as early as JDK 1.8, as follows:

  • 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 etc.

Newly added function interface of JDK 1.8 version:

  • java.util.function

java.util.function can be used to support functional programming in Java. The functional interfaces in this package are:

Serial number

Interface & description

1

BiConsumer<T,U> represents an operation that accepts two input parameters and does not return any results

2

BiFunction<T,U,R> represents a method that accepts two input parameters and returns a result

3

BinaryOperator<T> represents an operation that acts on two operators of the same type, and returns the result of the same type of operator

4

BiPredicate<T,U> represents a boolean value method with two parameters

5

BooleanSupplier represents the provider of boolean value results

6

Consumer<T> represents an operation that accepts an input parameter and has no return

7

DoubleBinaryOperator represents the operation that acts 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 accepts a double value parameter and returns the result

10

DoublePredicate represents a boolean value method with a double value parameter

11

DoubleSupplier represents the provider of a double value structure

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 the same 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 of the same type int, and the return value type is also int.

17

IntConsumer accepts an input parameter of type int and has 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 has no parameters and 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 accepts a parameter of the same type int, and the return value type is also int.

24

LongBinaryOperator accepts two parameters of the same type long, and the return value type is also long.

25

LongConsumer accepts an input parameter of type long and has no return value.

26

LongFunction<R> accepts a long type input parameter and returns a result.

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 a variety of default methods to combine Predicate into other complex logic (for example: AND, OR, NOT).

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

We can use the following example (Java8FunctionTest.java) to understand the use of the functional interface Predicate <T>:

package com.wenxue.jdk8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @className: FunctionTest
 * @Description: TODO tests the functional interface of java1.8
 * @version: v1.9.3
 * @author: GONGWENXUE
 * @date: 2019/12/11 15:39
 */
public class Java8FunctionTest {

   /**
     * @Author GONGWENXUE
     * @Description//TODO tests the Predicate<T> functional interface, passes in a parameter n, and returns a boolean result.
     * @version: v1.9.3
     * @Date 15:50 2019/12/11
     * @Param 
     * @return 
     **/
    public static void testFun(List<Integer> list, Predicate<Integer> predicate) {
        for(Integer n: list) {
           //When the test(n) of the function interface object is actually executed, the value of n is specifically passed in.
           //The previous expression (n-> n%2 == 0) is the implementation logic of the test method of the functional interface.
            if(predicate.test(n)) {
                System.out.println(n + "");
            }
        }
    }

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        System.out.println("Output all even numbers:");

       //Predicate<Integer> predicate1 = n -> operation expression (n%2==0)
       //n is a parameter passed to the test method of the Predicate interface
       //n%2==0: is the implementation logic of the test method, here is the use of Lam expressions to achieve the creation of objects and the implementation of abstract methods
       //When calling the method and passing in n-> n%2 == 0, the object of the functional interface and the rewriting of the test method have been implemented
       //Then call the test method, the rewriting logic of the test method, and return true or false
       //n-> n%2 == 0: The test method that identifies the functional interface needs to return true when the parameter value is an even number.
        testFun(list, n-> n%2 == 0 );//2, 4, 6

        System.out.println("Output all numbers greater than 3:");
        testFun(list, n-> n> 3 );//4, 5, 6
    }

}

Custom functional interface:

1. Write the functional interface first:

package com.wenxue.jdk8;

/**
 * @className: MyFunctionalInterface
 * @Description: TODO custom functional interface
 * @version: v1.9.3
 * @author: GONGWENXUE
 * @date: 2019/12/11 21:14
 */
@FunctionalInterface
public interface MyFunctionalInterface<T> {
    
    Integer test(T t, T t2);
   //Functional interface has one and only one abstract method, @FunctionalInterface will report an error when writing multiple
   //Integer test2(T t, T t2);
}

2. Create interface objects and implement abstract methods:

 MyFunctionalInterface<Integer> myFunctionalInterface = (n,n2) -> n + n2;

3. Call the function interface method:

package com.wenxue.jdk8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @className: FunctionTest
 * @Description: TODO tests the functional interface of java1.8
 * @version: v1.9.3
 * @author: GONGWENXUE
 * @date: 2019/12/11 15:39
 */
public class Java8FunctionTest {

      public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3);
   //Create a functional interface object, and implement the logic of the test method to add the two parameters and return
        MyFunctionalInterface<Integer> myFunctionalInterface = (n,n2) -> n + n2;
       //Call the functional interface method to execute the operation logic
        testMyFunctionalInterface(list, myFunctionalInterface);
    }
  
    public static void testMyFunctionalInterface(List<Integer> list, MyFunctionalInterface<Integer> myFunctionalInterface) {
        for(Integer n: list) {
            Integer result = myFunctionalInterface.test(n,n);
            System.out.println(result);//2,4,6
        }
    }

}
The difference between function interface and ordinary interface:
A functional interface can only have one abstract method (not including inherited or overridden Object methods), and a common interface can have multiple abstract methods.
@FunctionalInterface
public interface MyFunctionalInterface<T> {

    Integer test(T t, T t2);
   //Functional interface has one and only one abstract method, writing more than one will report an error
   //Integer test2(T t, T t2);

    @Override//This equals method is inherited from Object, here just means rewriting, so it is not an extra abstract interface
    boolean equals(Object obj);
}