Lambda表达式过滤数组

2021-01-29 17:13

阅读:772

标签:zha   lambda表达式   UNC   lte   san   数组   print   image   表达   

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 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 findByName(String name,List personList){
       return personList.stream().filter(person -> person.getName().equals(name)).collect(Collectors.toList());
    }

    public List findMoreThanAge(int age, ListpersonList, BiFunction,List> biFunction){
        return biFunction.apply(age,personList);
    }
}

技术图片

Lambda表达式过滤数组

标签:zha   lambda表达式   UNC   lte   san   数组   print   image   表达   

原文地址:https://www.cnblogs.com/zhvip/p/12831235.html


评论


亲,登录后才可以留言!