Collection<Person> collection = new ArrayList();
collection.add(new Person(1, "张三", 22, "男"));
collection.add(new Person(2, "李四", 19, "女"));
collection.add(new Person(3, "王五", 34, "男"));
collection.add(new Person(4, "赵六", 30, "男"));
collection.add(new Person(5, "田七", 25, "女"));
//简单逻辑
collection.removeIf(p -> p.getAge() >= 30);//过滤30岁以上的求职者
//复杂逻辑过滤
collection.removeIf(new Predicate<Person>() {
@Override
public boolean test(Person p) {
int id = p.getId();
Person children = getChildrenByParentId(id);
//过滤没有孩子的数据
if(children == null ){
return true;
}else{
return false;
}
}
});
java8 数据集removeIf过滤
于 2020-08-12 17:38:01 首次发布