java8+stream+remove_java8 数据集过滤removeIf和filter

http://www.cnblogs.com/tiandi/p/11185236.html

对象如下,需求:只要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 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 iterator = collection.iterator();

while (iterator.hasNext()) {

Person p = iterator.next();

if (p.getAge() >= 30)

iterator.remove();

}

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

2、不用lambda的removeIf写法

Collection 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() {

@Override

public boolean test(Person p) {

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

}

});

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

3、使用lambda的removeIf写法(只有一行了,哈哈)

Collection 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 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 personStream = collection.stream().filter(p -> p.getAge() < 30)).collect(Collectors.toList());

//由于使用了stream因此后面需要使用.collect(Collectors.toList())转换成list

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

下面是没有使用lambda的写法

Collection 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 personStream = collection.stream().filter(new Predicate() {

@Override

public boolean test(Person p) {

return p.getAge() < 30;//只保留男性

}

});

collection = personStream.collect(Collectors.toList());//将Stream转化为List

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

转载于:https://www.cnblogs.com/tiandi/p/11185236.html

标签:30,collection,removeIf,filter,Person,add,toString,new,java8

来源: https://blog.csdn.net/weixin_30294709/article/details/98189344

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值