实例Bean
package com.atguigu.cachestudy.bean;
import lombok.Data;
@Data
public class Emplyer {
private int id;
private String name;
private double salary;
private int age;
public Emplyer() {
super();
}
public Emplyer(int id, String name) {
this.id = id;
this.name = name;
}
public Emplyer(int id) {
this.id = id;
System.out.println( "Emplyer(int id)" );
}
public Emplyer(int id, String name, double salary, int age) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public void testEmplyer() {
System.out.println( "hello world!!!!" );
}
}
测试代码
package function;
import com.atguigu.cachestudy.bean.Emplyer;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.*;
public class Function003 {
@Test
public void test001() {
Comparator<String> comparator = (s1, s2) -> s1.compareTo( s2 );
System.out.println( comparator.compare( "ab", "abd" ) );
System.out.println( "*****************" );
Comparator<String> comparator2 = String::compareTo;
System.out.println( comparator2.compare( "ab", "abc" ) );
}
@Test
public void test002() {
BiPredicate<String, String> biPredicate1 = (s1, s2) -> s1.equals( s2 );
System.out.println( biPredicate1.test( "abc", "abc" ) );
System.out.println( "************************" );
BiPredicate<String, String> biPredicate2 = String::equals;
System.out.println( biPredicate2.test( "abc", "abd" ) );
}
@Test
public void test003() {
Emplyer em = new Emplyer( 2, "zhangsan", 11000.5, 30 );
Function<Emplyer, String> fuc = e -> e.getName();
System.out.println( fuc.apply( em ) );
System.out.println( "***********************************" );
Function<Emplyer, String> fuc2 = Emplyer::getName;
System.out.println( fuc2.apply( em ) );
}
@Test
public void test004() {
Emplyer em = new Emplyer( 2, "zhangsan", 11000.5, 30 );
Consumer<Emplyer> con = e -> e.testEmplyer();
con.accept( em );
System.out.println( "*********************" );
Consumer<Emplyer> con1 = Emplyer::testEmplyer;
con1.accept( em );
}
// 无参构造器引用示例
@Test
public void test005() {
Supplier<Emplyer> supplier = () -> new Emplyer();
System.out.println( supplier.get() );
System.out.println( "***************************" );
Supplier<Emplyer> supplier1 = Emplyer::new;
System.out.println( supplier1.get() );
}
// 有参构造器引用示例
@Test
public void test006() {
Function<Integer, Emplyer> func = (id) -> new Emplyer( id );
Emplyer emplyer = func.apply( 1001 );
System.out.println( emplyer );
System.out.println( "*******************" );
Function<Integer, Emplyer> func1 = Emplyer::new;
Emplyer emplyer1 = func1.apply( 1002 );
System.out.println( emplyer1 );
System.out.println( "*******************" );
BiFunction<Integer, String, Emplyer> func2 = Emplyer::new;
Emplyer emplyer2 = func2.apply( 1003, "张三" );
System.out.println( emplyer2 );
}
/**
* 数组引用示例
*/
@Test
public void test007() {
Function<Integer, String[]> fun = (id) -> new String[id];
String[] str = fun.apply( 5 );
System.out.println( Arrays.toString( str ) );
System.out.println( "**********************" );
Function<Integer, String[]> fun1 = String[]::new;
String[] str1 = fun1.apply( 5 );
System.out.println( Arrays.toString( str1 ) );
}
}