filter
使用Stream的filter进行过滤,只保留男性的操作:
Collection<Person> collection = new ArrayList();collection.add(new Person("张三", 22, "男"));collection.add(new Person("李四", 19, "女"));collection.add(new Person("王五", 34, "男"));collection.add(new Person("赵六", 30, "男"));collection.add(new Person("田七", 25, "女"));Stream<Person> personStream = collection.stream().filter(new Predicate<Person>() {@Overridepublic boolean test(Person person) {return "男".equals(person.getGender());//只保留男性}});collection = personStream.collect(Collectors.toList());//将Stream转化为ListSystem.out.println(collection.toString());//查看结果
