public class Person {
private int age;
private String name;
public Person(String name,int age) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
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);
Person person4 = new Person("zhaoliu",50);
List<Person> list = Arrays.asList(person1,person2,person3,person4);
new PersonTest().findByName("zhangsan",list).forEach(person -> System.out.println(person));
System.out.println("--------------------");
new PersonTest().findMoreThanAge(20,list,(personAge , personList)->{
return personList.stream().filter(person -> person.getAge() > personAge).collect(Collectors.toList());
}).forEach(person -> System.out.println(person));
}
public List<Person> findByName(String name,List<Person> personList){
return personList.stream().filter(person -> person.getName().equals(name)).collect(Collectors.toList());
}
public List<Person> findMoreThanAge(int age, List<Person>personList, BiFunction<Integer,List<Person>,List<Person>> biFunction){
return biFunction.apply(age,personList);
}
}
![]()