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'}