package com.zs.boot.controller;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.Test;
public class TestMethodRef {
public class Employee{
private String name;
private int age;
private double salary;
public Employee() {
}
public Employee(int age) {
this.age = age;
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public Employee(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String show() {
return "测试方法引用!";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + ''' +
", age=" + age +
", salary=" + salary +
'}';
}
}
List employees = Arrays.asList(
new Employee("刘德华",48,8000),
new Employee("张学友",28,8500),
new Employee("黎明",45,6500),
new Employee("郭富城",51,5500));
@Test
public void test1(){
Consumer con = (str) -> System.out.println(str);
con.accept("你好!");
PrintStream ps = System.out;
Consumer con1 = ps::println;
con1.accept("大家好");
//最终版,对象的引用 :: 实例方法名
Consumer con2 = System.out::println;
con2.accept("Hello Java8!");
}
//--------------------------------------------------------------------
//对象的引用 :: 实例方法名
@Test
public void test2(){
Employee emp = new Employee("周华健", 55, 9999);
Supplier sup = () -> emp.getName();
String str = sup.get();
System.out.println(str);
System.out.println("----------------------------------");
Supplier sup2 = emp::getName;
System.out.println(sup2.get());
}
//--------------------------------------------------------------------
//类名 :: 静态方法名
@Test
public void test3(){
Comparator com = (x, y) -> Integer.compare(x, y);
int compare1 = com.compare(12,21); //-1
System.out.println(compare1);
System.out.println("-------------------------------------");
Comparator com2 = Integer::compare;
int compare2 = com.compare(12,21); //-1
System.out.println(compare2);
}
//--------------------------------------------------------------------
//类名 :: 实例方法名
@Test
public void test4(){
BiPredicate bp = (x, y) -> x.equals(y);
System.out.println(bp.test("abcde", "abcde"));
//若Lambda的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName
//(x, y) -> x.equals(y)
BiPredicate bp2 = String::equals;
System.out.println(bp2.test("abc", "abc"));
System.out.println("-----------------------------------------");
Function fun = (e) -> e.show();
System.out.println(fun.apply(new Employee()));
Function fun2 = Employee::show;
System.out.println(fun2.apply(new Employee()));
}
@Test
public void test5(){
BiFunction fun = (x, y) -> Math.max(x, y);
System.out.println(fun.apply(1.5, 22.2));
System.out.println("--------------------------------------------------");
BiFunction fun2 = Math::max;
System.out.println(fun2.apply(1.2, 1.5));
}
//--------------------------------------------------------------------
//构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!
@Test
public void test6(){
Supplier sup = () -> new Employee();
System.out.println(sup.get());//Employee{name='null', age=0, salary=0.0}
System.out.println("------------------------------------");
Supplier sup2 = Employee::new; //匹配无参构造器
Employee emp = sup2.get();//返回一个对象
System.out.println(emp);//Employee{name='null', age=0, salary=0.0}
}
@Test
public void test7(){
Function fun = (x) -> new Employee();//匹配一个参数的构造器
Function fun2 = Employee :: new;//匹配一个参数的构造器
Employee emp = fun2.apply(1000);
System.out.println(emp);
//传两个参数,必须要有两个参数的构造器
BiFunction fun3 = Employee::new;//匹配两个参数的构造器
}
//--------------------------------------------------------------------
//数组引用
@Test
public void test8(){
Function fun = (args) -> new String[args];
String[] strs = fun.apply(10);
System.out.println(strs.length);//数组长度为10
System.out.println("--------------------------");
Function fun2 = Employee[] :: new;
Employee[] emps = fun2.apply(20);
System.out.println(emps.length);//数组长度为20
}
}