1、如下:用户对象
public class User(){
private String name;
private int age;
private String sex;
}
2、构造用户集合
List<User> list = new ArrayList<>();
User user1 = new User("张三",15,"男");
User user2 = new User("李四",22,"男");
User user3 = new User("王五",25,"男");
User user4 = new User("赵六",30,"男");
User user5 = new User("李世民",28,"男");
User user6 = new User("杨贵妃",18,"女");
User user7 = new User("貂蝉",16,"女");
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
list.add(user5);
list.add(user6);
list.add(user7);
3、过滤
(1)、单一条件过滤:根据对象中的一个条件过滤(过滤名字不为张三的所有人)
List<User> collect = list.stream()
.filter(user -> !"张三".equals(user.getName()))
.collect(Collectors.toList());
(2)、多条件过滤:根据对象中的多个属性过滤(过滤,只剩年龄在30岁以下,并且性别为女的用户)
List<User> collect = list.stream()
.filter(user -> user.getAge()<30 && "女".equals(user.getSex()))
.collect(Collectors.toList());