我们通常会将一个集合中不符合条件的对象过滤掉,只保留我们需要的对象。
代码示例:
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(new Student("张三", 19, "杭州"));
list.add(new Student("李四", 18, "北京"));
list.add(new Student("王五", 17, "西安"));
list.add(new Student("赵六", 16, "兰州"));
List studentList = list.stream().filter(student -> student.getAge() > 17)
.collect(Collectors.toList());
System.out.println(studentList);
}
原理描述:
list集合中有四位同学,用方法 .filter(条件表达式)
过滤年龄大于17岁的。
结果:(结果中并没有出现小于等于17岁的同学)
[Student{name='张三', age=19, addr='杭州'}, Student{name='李四', age=18, addr='北京'}]
Original: https://www.cnblogs.com/CodeLuckly/p/16859916.html
Author: CodeLuckly
Title: JAVA8-Lambda-Filter过滤
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/713673/
转载文章受原作者版权保护。转载请注明原作者出处!