package com.example.demo.java8;
import org.junit.Test;
import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class TestMethodReference {
@Test
public void test1(){
Consumer con1 = str->{System.out.println(str);};
con1.accept("1123333");
PrintStream ps = System.out;
Consumer<String> con2 = ps::println;
con2.accept("wwwwww");
}
@Test
public void test2(){
Employee employee = new Employee(12,"单点的",25d);
Supplier<String> s1 = () -> employee.getName();
System.out.println(s1.get());
Supplier<String> s2 = employee::getName;
System.out.println(s2.get());
}
@Test
public void test3(){
Comparator<Integer> com1 = (t1,t2)-> Integer.compare(t1, t2);
System.out.println(com1.compare(12, 34));
Comparator<Integer> com2 = Integer::compare;
System.out.println(com2.compare(78, 68));
}
@Test
public void test4(){
Function<Double,Long> fun1 = d -> Math.round(d);
System.out.println(fun1.apply(14d));
Function<Double,Long> fun2 = Math::round;
System.out.println(fun2.apply(14d));
}
@Test
public void test5(){
BiPredicate<String,String> bp1 = (t1,t2) -> {return t1.equals(t2);};
System.out.println(bp1.test("11", "222"));
BiPredicate<String,String> bp2 = String::equals;
System.out.println(bp2.test("11", "222"));
}
@Test
public void test6(){
Employee employee = new Employee(15, "我顶顶顶", 25d);
Function<Employee,String> fun1 = (a) -> a.getName();
System.out.println(fun1.apply(employee));
Function<Employee,String> fun2 = Employee::getName;
System.out.println(fun2.apply(employee));
}
}