Topic 4 of new features of Java 8: lambda method reference and constructor reference

Posted by slobodnium on Sat, 05 Mar 2022 17:12:32 +0100

  • landscape
    Java 8's lambda introduces three method references, constructor references, and array references to reduce code development. It should be noted that if you need to use this feature, the method return value type, parameter number and type must be consistent with the functional interface.
  • example
  1. Object instance method usage scenario
    This feature can be used when the instance method of the object has implemented the functions to be implemented by the functional interface, and the parameter number, parameter type and return type of the instance method are consistent with those of the functional interface. Usage:
    Object:: method name
/**
 * @author: zhuocc2
 * @date: 2019-09-02 4:53:18 PM
 * @Package: lambda
 * @Description: Object instance method usage scenario.
 *  This method can be used when the instantiated method has realized the functions to be realized by lambda.
 *  However, the parameters and return values of lambda functional interface are the same as those of instance method
 *  Usage object:: method name
 */
@Test
public void objectInstance() {
    PrintStream sysOut = System.out;
        
    Consumer<String> consumer = sysOut::println;
    consumer.accept("object-Application of case method");
}

output
Application of object instance method

  1. Class - static method usage scenario
    This feature can be used when the static method of the class has implemented the functions to be implemented by the functional interface, and the parameter quantity, parameter type and return type of the instance method are consistent with those of the functional interface. Usage:
    Class:: method name
/**
 * @author: zhuocc2
 * @date: 2019-09-02 5:27:39 PM
 * @Package: lambda
 * @Description: TODO Class - static method usage scenario
 *  This method can be used when the functions to be implemented by lambda have been implemented in the class. Just keep the parameters and return consistent with the lambda functional interface.
 *  Usage: Class Name:: method name
 */
@Test
public void classStaticMethod() {
    Comparator<Integer> comparator = Integer::compare;
    System.out.println(comparator.compare(2, 1));
}

output
1

  1. Class instance method usage scenario
    This feature can be used when the first parameter of the instance method is the caller of the instance method and the second parameter is the parameter of the instance method, and the number and type of parameters and return value type of the instance method are consistent with those of the functional interface. Usage:
    Class:: method name
@Test
public void classInstance() {
    BiPredicate<String, String> bp = String::equals;
        
    boolean test = bp.test("2", "2");
    System.out.println(test);
}

output
true

  1. Constructor reference usage scenario
    This feature can be used when the constructor's parameter list is consistent with the parameter list of a functional interface. Usage:
public class TestConstruct {

    private String str = null;
    
    public TestConstruct(String str) {
        this.str = str;
        System.out.println(str);
    }
    
    public TestConstruct(String str1, String str2) {
        System.out.println(str1 + str2);
    }
    
    public String getStr() {
        return this.str;
    }
}
/**
 * @author: zhuocc2
 * @date: 2019-09-03 9:24:04 PM
 * @Package: lambda
 * @Description: TODO Constructor reference
 */
@Test
public void constructReference() {
    Function<String, TestConstruct> fun = TestConstruct::new;
    TestConstruct construct = fun.apply("str");
    System.out.println(construct.getStr());
}
output
str
str
  1. Array reference
/**
 * @author: zhuocc2
 * @date: 2019-09-03 9:30:22 PM
 * @Package: lambda
 * @Description: TODO Array reference
 */
@Test
public void arrayReference() {
        
    Function<Integer, Float[]> fun = Float[]::new;
    Float[] array = fun.apply(20);
    System.out.println(array.length);   
}

output
20

1, Method reference: if any method in Lambda body has been implemented, we can use "method reference"

The parameter and return value types of the method are required to be consistent with the parameter and return value types in the functional interface.

There are three main syntax formats:

Object:: instance method name

Consumer<String> consumer1 = System.out::print;   //Implemented by method reference Lambda Body.
consumer1.accept("Hello World!");

Class:: static method name

Comparator<Integer> comparator1 = Integer::compare;  Equivalent to
Comparator<Integer> comparator2 = (x, y) -> Integer.compare(x,y);

Class:: instance method name

//Lambda When the first parameter in the expression parameter list is the caller of the instance method and the second parameter is the parameter of the instance method, the class name can be used :: Method name
BiPredicate<String, String> biPredicate = (x,y) -> x.equals(y);
BiPredicate<String,String> biPredicate1 = String :: equals;      
 

2. Constructor reference: the parameter type in the constructor is consistent with that in the functional interface.

Supplier<Date> supplier = Date :: new;
Date date = supplier.get();
long time = date.getTime();

3, Array reference

Function<Integer, String[]> function =  String[]::new;
String[] apply = function.apply(10);
System.out.println(apply.length); // 10

Author: Seventeen cats
Link: https://www.jianshu.com/p/f2ab2db6fe2b
Source: Jianshu

 

Topics: java8