1.输出符合表达式的每一个对象
employees
.stream().filter(p
-> p
.getAge() > 21).forEach(System
.out
::println
);
2.返回一个符合表达式的集合
Stream
<Person> personStream
= collection
.stream().filter(new Predicate<Person>() {
@Override
public boolean test(Person person
) {
return "男".equals(person
.getGender());
}
});
collection
= personStream
.collect(Collectors
.toList());
System
.out
.println(collection
.toString());
3.返回符合表达式的集合的第一个对象
employees
.stream().filter(p
-> p
.getAge() > 21).findFirst();
4.抽取对象中所有的id的集合
employees
.stream().map(Employee
::getId
).collect(Collectors
.toList());
转载请注明原文地址: https://mac.8miu.com/read-491482.html