I have a Set<Person> and I need to filter the content and then populate another set out of the given set.
I am fairly new to streams.
The first solution that I can think of is chaining stream().filter().foreach()
Solution 1:
private Set<Person> mapPerson(final Set<Student> student) {
final Set<Person> person = new HashSet<>();
student.stream()
.filter(student1 -> Objects.nonNull(student.getAge()))
.forEach(student1 -> person.add(Person.builder()
.id(student1.getId())
.status(student1.getStatus()))
.build()));
return person;
}
But I am not sure if this can somehow be done by chaining stream().filter().map() ?
Would appreciate any help and also if it is possible to do it both ways, which is the preferred way?