2020你还不会Java8新特性? (3)

BiFunction: 整合两个函数的方法。
为什么BiFunction不提供 compose ,只提供andThen呢?
因为如果提供compose方法的话,只能获取一个参数的返回值。不合理。

public static void main(String[] args) { FunctionTest2 functionTest2 = new FunctionTest2(); // compose // System.out.println(functionTest2.compute(2,a -> a * 3,b -> b * b)); // andThen // System.out.println(functionTest2.compute2(2,a -> a * 3,b -> b * b)); //BiFunction // System.out.println(functionTest2.compute3(1,2, (a,b) -> a - b)); // System.out.println(functionTest2.compute3(1,2, (a,b) -> a * b)); // System.out.println(functionTest2.compute3(1,2, (a,b) -> a + b)); // System.out.println(functionTest2.compute3(1,2, (a,b) -> a / b)); //BiFunction andThen System.out.println(functionTest2.compute4(2,3,(a,b) ->a + b , a -> a * a )); } //compose : 组合function, 形成两个function的串联。 先执行参数 //andThen :先应用当前的函数apply,然后再当做参数再次执行apply。 后执行参数 public int compute(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) { return function1.compose(function2).apply(a); } public int compute2(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) { return function1.andThen(function2).apply(a); } //BiFunction //求两个参数的和 //先定义一个抽象的行为. public int compute3(int a, int b, BiFunction<Integer, Integer, Integer> biFunction) { return biFunction.apply(a, b); } //BiFunction andThen public int compute4(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function) { return biFunction.andThen(function).apply(a, b); }

测试 函数式接口的实例:

public class PersonTest { public static void main(String[] args) { List<Person> personList = new ArrayList<>(); personList.add(new Person("zhangsan", 20)); personList.add(new Person("zhangsan", 28)); personList.add(new Person("lisi", 30)); personList.add(new Person("wangwu", 40)); PersonTest test = new PersonTest(); //测试 getPersonUsername // List<Person> personList1 = test.getPersonUsername("zhangsan", personList); // personList1.forEach(person -> System.out.println(person.getUsername())); //测试 getPersonByAge List<Person> personByAge = test.getPersonByAge(25, personList); personByAge.forEach(person -> System.out.println(person.getAge())); //测试第三种: 自定义输入行为 List<Person> list = test.getPersonByAge2(20,personList,(age,persons) ->{ return persons.stream().filter(person -> person.getAge() > age).collect(Collectors.toList()); }); list.forEach(person -> System.out.println(person.getAge())); } public List<Person> getPersonUsername(String username, List<Person> personList) { return personList.stream().filter(person -> person.getUsername().equals(username)).collect(Collectors.toList()); } public List<Person> getPersonByAge(int age, List<Person> personList) { //使用BiFunction的方式 // BiFunction<Integer, List<Person>, List<Person>> biFunction = (ageOfPerson, list) -> { // return list.stream().filter(person -> person.getAge() > ageOfPerson ).collect(Collectors.toList()); // }; //变换之后: BiFunction<Integer, List<Person>, List<Person>> biFunction = (ageOfPerson, list) -> list.stream().filter(person -> person.getAge() > ageOfPerson ).collect(Collectors.toList()); return biFunction.apply(age, personList); } //第三种方式, 动作也让用户自己定义传进来 public List<Person> getPersonByAge2(int age ,List<Person> list,BiFunction<Integer,List<Person>,List<Person>> biFunction){ return biFunction.apply(age, list); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwgpf.html