使用情况:当传递的Lambda体的操作,已经有实现的方法了,可以使用方法引用
使用格式:类(或对象)::方法名
1:对象::非静态方法
2:类::静态方法
3:类::非静态方法
/**
* 描述
* @version 1.0
* @date 2022/05/14 22:43:35
*/
public class Student {
private Integer id;
private String name;
public Student() {
}
public Student(Integer id) {
this.id = id;
}
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
/**
* 使用方法引用的方式
* 1:对象::非静态方法
*/
@Test
public void test1() {
Consumer<String> consumer = System.out::println;
consumer.accept("测试方法引用");
System.out.println("**************************");
Student student = new Student(1, "李四");
MyGet<String> name = student::getName;
System.out.println(name.get());
}
运行结果:
测试方法引用
**************************
李四
/**
* 自定义函数式接口,只有一个抽象方法
*
* @version 1.0
* @date 2022/05/14 19:21:05
*/
@FunctionalInterface
public interface MyInterface2<T> {
T add(T a, T b);
}
/**
* 2:类::静态方法
*/
@Test
public void test2() {
MyInterface2<Integer> myInterface = Integer::sum;
Integer add = myInterface.add(1, 2);
System.out.println(add);
System.out.println("**************************");
Comparator<Integer> comparator = Integer::compare;
int com = comparator.compare(12, 23);
System.out.println(com);
}
运行结果:
3
**************************
-1
/**
* 3:类::静态方法
*/
@Test
public void test3() {
Function<Double, Long> fun1 = d -> Math.round(d);
Long apply = fun1.apply(12.4);
System.out.println(apply);
System.out.println("**************************");
Function<Double, Long> fun2 = Math::round;
Long apply1 = fun2.apply(13.3);
System.out.println(apply1);
}
运行结果:
12
**************************
13
/**
* 4:类::实例方法
*/
@Test
public void test4() {
Comparator<String> comparator = (s1, s2) -> s1.compareTo(s2);
int compare = comparator.compare("abc", "abd");
System.out.println(compare);
System.out.println("**************************");
Comparator<String> comparator1 = String::compareTo;
int compare1 = comparator1.compare("abe", "abd");
System.out.println(compare1);
}
运行结果:
-1
**************************
1
/**
* 5:类::非静态方法
*/
@Test
public void test5() {
Student student = new Student(1, "Tom");
Function<Student, String> function = Student::getName;
String name = function.apply(student);
System.out.println(name);
}
运行结果:
Tom
/**
* 构造器引用
*/
@Test
public void test6() {
//空参构造器
Supplier<Student> supplier = Student::new;
Student student = supplier.get();
System.out.println(student);
System.out.println("**************************");
//只有id的构造器
Function<Integer, Student> function = Student::new;
Student student1 = function.apply(1001);
System.out.println(student1);
System.out.println("**************************");
//两个参数的构造器
BiFunction<Integer, String, Student> biFunction = Student::new;
Student jerry = biFunction.apply(1002, "Jerry");
System.out.println(jerry);
}
运行结果:
Student{id=null, name='null'}
**************************
Student{id=1001, name='null'}
**************************
Student{id=1002, name='Jerry'}
/**
* 数组引用
*/
@Test
public void test7() {
Function<Integer, String[]> function = String[] :: new;
String[] apply = function.apply(10);
System.out.println(Arrays.toString(apply));
}
运行结果:
[null, null, null, null, null, null, null, null, null, null]