java8 數據集過濾removeIf和filter


對象如下,需求:只要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());//查看結果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM