You can flatMapeach student in a stream formed by the student along with his teachers:
HashSet<Person> combined =
students.stream()
.flatMap(student -> Stream.concat(Stream.of(student), student.getTeachers().stream()))
.collect(Collectors.toCollection(HashSet::new));
concatused to unite in the stream of Masters, a stream formed by the student himself, obtained with the help of of.
source
share