BiFunction
引申 BiFunction 类似于Function 这里西药传入两个行为参数
例7BiFunction与Function默认方法的使用
public class FunctionTest2 {
public static void main(String[] args) {
FunctionTest2 test = new FunctionTest2();
System.out.println(test.compose(10, x -> x * 3, y -> y * y)); //300
System.out.println(test.compose2(10, x -> x * 3, y -> y * y)); //900
//输入的行为的不同 返回的结果不同
System.out.println(test.compose3(2, 3, (x, y) -> x + y));//5
System.out.println(test.compose3(2, 3, (x, y) -> x - y));//-1
System.out.println(test.compose3(2, 3, (x, y) -> x * y));//6
System.out.println(test.compose3(2, 3, (x, y) -> x / y));//0
System.out.println(test.compose4(2, 4, (x, y) -> x + y, x -> x * x));//36
}
public int compose(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.compose(function2).apply(a);
}
public int compose2(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
return function1.andThen(function2).apply(a);
}
public int compose3(int a, int b, BiFunction<Integer, Integer, Integer> biFunction) {
//参数怎么执行是由返回结果根据用户传过来的的行为决定
return biFunction.apply(a, b);
}
public int compose4(int a, int b, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function) {
//此时调用apply()相当于当前的函数式接口的参数传值 行为就是他的实现
return biFunction.andThen(function).apply(a, b);
}
}
例8 BiFunction需求使用
对象
@Data
public class Person {
private String username;
private int age;
}
public class PersonTest {
public static void main(String[] args) {
Person person1 = new Person("zhangsan", 20);
Person person2 = new Person("lisi", 30);
Person person3 = new Person("wangwu", 40);
List<Person> list = Arrays.asList(person1, person2, person3);
PersonTest test = new PersonTest();
// List<Person> person = test.listPersonByname("zhangsan", list);
// person.forEach(x -> System.out.println(x));
/*
此时行为是写死的 只能获取大于20 的List<Person> 集合
*/
// System.out.println(test.listPersonByAge(20, list));
//不同行为返回的需求结果不同 具体需求是调用者决定的
List<Person> listPerson = test.ListPersonByAge2(20, list, (x, y) ->
y.stream().filter(person -> person.getAge() > x).collect(Collectors.toList()));
System.out.println(listPerson);
System.out.println("=====================================================");
List<Person> listPerson2 = test.ListPersonByAge2(20, list, (x, y) ->
y.stream().filter(person -> person.getAge() <= x).collect(Collectors.toList()));
System.out.println(listPerson2);
}
public List<Person> listPersonByname(String username, List<Person> list) {
/*
将List转换成流 流里每个对象都是Person对象 判断为true的流收集返回
*/
return list.stream().filter(x -> username.equals(x.getUsername()))
.collect(Collectors.toList());
}
public List<Person> listPersonByAge(Integer age, List<Person> list) {
//{}写带返回结果的语句,需 加上return不然会报错
BiFunction<Integer, List<Person>, List<Person>> biFunction = (x, y) ->
y.stream().filter(person -> person.getAge() > age).collect(Collectors.toList());
return biFunction.apply(age, list);
}
public List<Person> listPersonByAge2(Integer age, List<Person> personList, BiFunction<Integer, List<Person>, List<Person>> biFunction) {
return biFunction.apply(age, personList);
}
}
Predicate
public class PredicateTest {
public static void main(String[] args) {
/*
可以看做过滤 就是根据行为动作去返回为true的值 filter接口行为参数就是该函数式接口
*/
Predicate<String> predicate = x -> x.length() > 5;
System.out.println(predicate.test("nihaoa")); //true
}
}