public class TestMethodRef {
@Test
public void test1(){
PrintStream ps=System.out;
Consumer<String> consumer=(x)->ps.println(x);
consumer.accept("Hello World");
PrintStream ps1=System.out;
Consumer<String> consumer1=ps1::println;
consumer1.accept("Hello World");
}
@Test
public void test2(){
Employee employee=new Employee("张三",18,9999.99);
Supplier<String> supplier=()->employee.getName();
System.out.println(supplier.get());
Supplier<String> supplier1=employee::getName;
System.out.println(supplier1.get());
}
@Test
public void test3(){
Comparator<Integer> comparator=(x,y)->Integer.compare(x,y);
Comparator<Integer> comparator1=Integer::compare;
System.out.println(comparator1.compare(1,2));
}
@Test
public void test4(){
BiPredicate<String,String> predicate=(x,y)->x.equals(y);
BiPredicate<String,String> predicate1=String::equals;
System.out.println(predicate1.test("Hello","Hello"));
}
@Test
public void test5(){
Supplier<Employee> supplier=()->new Employee();
Supplier<Employee> supplier1=Employee::new;
System.out.println(supplier1.get() instanceof Employee);
}
@Test
public void test6(){
Function<Integer,Employee> function=(x)->new Employee(x);
Function<Integer,Employee> function1=Employee::new;
System.out.println(function1.apply(18));
System.out.println(function1.apply(5).getName()==null);
}
@Test
public void test7(){
Function<Integer,String[]> function=(x)->new String[x];
Function<Integer,String[]> function1=String[]::new;
String[] str=function1.apply(10);
System.out.println(str.length);
}
}