This is the first day of my participation in Gwen Challenge
Accumulate over a long period, constant dripping wears away a stone 😄
What is a method reference
A method reference is another representation of a Lambda expression and is a syntactic sugar. So why use method references?
Method references can be used when an implementation in the Lambda body has already been implemented by other methods. That is, using method references can reduce code and increase productivity. When using method references, we need to ensure that the parameter list and return value type of the reference method are the same as the parameter list and return value type of the functional interface method we are currently implementing.
Syntax format
Object :: Instance method name
@Test
public void test1(a){
Consumer<String> con = (x) ->System.out.println(x);
// Execute the Lambda body
con.accept("gongj");
/ / short
Consumer<String> con2 = System.out::println;
con2.accept("yuanj");
}
Copy the code
Notice the premise of writing this:accept()
Methods andprintln()
The argument list of the method should be exactly the same as the return type:
Prepare an Employee object
public class Employee {
private String name;
private int age;
private double salary;
public Employee(a) {}public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee(String name) {
this.name = name;
}
public String getName(a) {
return name;
}
public int getAge(a) {
return age;
}
public double getSalary(a) {
return salary;
}
@Override
public String toString(a) {
return "name='" + name + '\' ' +
", age=" + age +
", salary="+ salary; }}Copy the code
Let’s do another example
@Test
public void test2(a){
Employee emp = new Employee("gongj".122.7888);
Supplier<String> sup = () -> emp.getName();
System.out.println(sup.get());
/ / short
Supplier<String> sup2 = emp::getName;
System.out.println(sup2.get());
}
Copy the code
Class :: Static method name
@Test
public void test3(a) {
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
com.compare(1.2);
// How to use method references
Comparator<Integer> com1 = Integer::compare;
com1.compare(1.2);
}
Copy the code
Class :: Instance method name
@Test
public void test4(){
BiPredicate<String,String> bp = (x,y) -> x.equals(y);
System.out.println(bp.test("gognj","dddd"));
BiPredicate<String,String> bp2 = String::equals;
System.out.println(bp.test("gognj","gognj"));
}
Copy the code
Use note: The class :: instance method name can be used when the first argument in the Lambda argument list is the caller of the instance method and the second argument is the argument of the instance method.
The way this method is referenced does not need to be satisfiedEnsure that the argument list and return value type of the reference method are the same as those of the functional interface method we are currently implementing
This rule
Constructor reference
Class ::new. Which constructor to call depends on the definition of the method parameter in the functional interface. Lambda will automatically infer which constructor to call from the interface method, that is, the constructor argument list needs to be the same as the argument list of the abstract method in the functional interface
- Call the no-parameter constructor
@Test
public void test5(a){
Supplier<Employee> sup = () -> new Employee();
// Constructor reference The no-parameter constructor is called here
Supplier<Employee> sup2 = Employee::new; System.out.println(sup2.get()); } result: name='null', age=0, salary=0.0
Copy the code
- Invokes the parameter constructor
@Test
public void test6(a){
Function<String,Employee> fun = (x) -> new Employee(x);
System.out.println(fun.apply("gongj"));
Function<String,Employee> fun2 = Employee::new;
System.out.println(fun2.apply("yuanj")); } result: name='gongj', age=0, salary=0.0
name='yuanj', age=0, salary=0.0
Copy the code
If you want to match multiple parameters (BiFunction can be used for two), look at a three parameter, custom one function interface:
@FunctionalInterface
public interface MyFun2<T,A,B, R> {
R apply(T t,A a,B b);
}
Copy the code
test
@Test
public void test7(a){
MyFun2<String,Integer, Double,Employee> fun = Employee::new;
System.out.println(fun.apply("gongj".99999d)); } result: name='gongj', age=25, salary=99999.0
Copy the code
An array reference
The syntax format is Type[]::new
@Test
public void test8(a) {
Function<Integer, String[]> fun = (x) -> new String[x];
String[] apply = fun.apply(10);
System.out.println(apply.length);
// Array reference
Function<Integer, String[]> fun1 = String[]::new;
String[] apply2 = fun1.apply(10); System.out.println(apply2.length); } the result:10
20
Copy the code
- If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.