java8的stream系列教程之filter过滤集合的一些属性
贴代码
- List<Student> lists = new ArrayList<>();
- Student student = new Student();
- student.setName("laoli");
- student.setNumber(1);
- lists.add(student);
- Student student1 = new Student();
- student1.setName("erzi");
- student1.setNumber(2);
- lists.add(student1);
- Student student3 = new Student();
- student3.setName("three");
- student3.setNumber(1);
- lists.add(student3);
- List<Student> collect = lists.stream().filter(s -> s.getNumber()==1&& !s.getName().equals("laoli")).collect(Collectors.toList());
- for (Student student2 : collect) {
- System.out.println(student2.getName());
- }
输出结果
three
主要代码:
List<Student> collect = lists.stream().filter(s -> s.getNumber()==1&& !s.getName().equals("laoli")).collect(Collectors.toList());
这个filter(s -> s.getNumber()==1&& !s.getName().equals("laoli")) 就是将你要过滤的对象取出来,设置过滤条件就好了。
这个设置条件的地方,就当成使用 if()时, 括号里面的内容。
附上student的类:
- public class Student{
-
- private String name;
- private Integer number;
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public Integer getNumber() {
- return number;
- }
-
- public void setNumber(Integer number) {
- this.number = number;
- }
- }
原文地址:https://blog.csdn.net/Mint6/article/details/84780487