【Java】【Collectors】removeIf、list删除元素

Collectors.removeIf

default boolean removeIf(Predicate<? super E> filter)

Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.

移除集合中符合入参条件的元素,在原集合上修改

应用:List筛选删除元素

List<Student> studentList = new ArrayList<>();
studentList.add(new Student("张一", 1, 13, "3"));
studentList.add(new Student("张二", 2, 13, "4"));
studentList.add(new Student("张三", 3, 14, "4"));
studentList.add(new Student("老王", 4, 14, "2"));
studentList.add(new Student("张四", 1, 15, "3"));
studentList.add(new Student("张五", 2, 16, "1"));
studentList.add(new Student("张六", 3, 17, "3"));
studentList.add(new Student("张七", 3, 18, "5"));
studentList.add(new Student("老王", 1, 15, "1"));
studentList.add(new Student("张八", 5, 15, "3"));
studentList.add(new Student("张九", 2, 15, null));
studentList.add(new Student("老王", 4, 13, null));

studentList.removeIf(s -> !Objects.nonNull(s.getClassNum()) || "3".equals(s.getClassNum()));
studentList.forEach(System.out::println);

output

Student{stuName='张二', stuId=2, stuAge=13, classNum='4'}
Student{stuName='张三', stuId=3, stuAge=14, classNum='4'}
Student{stuName='老王', stuId=4, stuAge=14, classNum='2'}
Student{stuName='张五', stuId=2, stuAge=16, classNum='1'}
Student{stuName='张七', stuId=3, stuAge=18, classNum='5'}
Student{stuName='老王', stuId=1, stuAge=15, classNum='1'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值