码迷,mamicode.com
首页 > 编程语言 > 详细

java8 数据集过滤removeIf和filter

时间:2019-07-14 19:26:16      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:bool   构造方法   arraylist   class   sys   ica   name   remove   哈哈   

对象如下,需求:只要30岁以下的人

//求职者的实体类
public class Person {
    private String name;//姓名
    private Integer age;//年龄
    private String gender;//性别

    ...
    //省略构造方法和getter、setter方法
    ...

    //重写toString,方便观看结果
    @Override
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", gender=‘" + gender + ‘\‘‘ +
                ‘}‘;
    }
}

1、使用Iterator的传统写法

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女"));
//过滤30岁以上的求职者
Iterator<Person> iterator = collection.iterator();
while (iterator.hasNext()) {
    Person p = iterator.next();
    if (p.getAge() >= 30)
        iterator.remove();
}
System.out.println(collection.toString());//查看结果

2、不用lambdaremoveIf写法

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女"));

collection.removeIf(new Predicate<Person>() {
    @Override
    public boolean test(Person p) {
        return p.getAge()>=30;//过滤30岁以上的求职者
    }
});

System.out.println(collection.toString());//查看结果

3、使用lambdaremoveIf写法(只有一行了,哈哈)

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女"));


collection.removeIf(p -> p.getAge() >= 30);//过滤30岁以上的求职者

System.out.println(collection.toString());//查看结果

4、使用lambda的filter写法

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女"));

Stream<Person> personStream = collection.stream().filter(p -> p.getAge() < 30)).collect(Collectors.toList());
//由于使用了stream因此后面需要使用.collect(Collectors.toList())转换成list

System.out.println(collection.toString());//查看结果

下面是没有使用lambda的写法

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女"));

Stream<Person> personStream = collection.stream().filter(new Predicate<Person>() {
    @Override
    public boolean test(Person p) {
         return p.getAge() < 30;//只保留男性
    }
});

collection = personStream.collect(Collectors.toList());//将Stream转化为List
System.out.println(collection.toString());//查看结果

 

java8 数据集过滤removeIf和filter

标签:bool   构造方法   arraylist   class   sys   ica   name   remove   哈哈   

原文地址:https://www.cnblogs.com/tiandi/p/11185236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!