Answer the question
In order to leave comments, you need to log in
How does Collector.of work?
Each student has a list of sections that he attends.
The groupingBy method has its accumulator through the Collector.of method. because the groupingBy method collects groups with the input collection type. In our case, only the names of students are needed there.
How does Collector.of work in a method that groups students into sections?
public static Map> sections(List students) {
return students.stream().flatMap(student -> student.getUnits().stream().map(unit -> new Holder(unit, student.getName()))) // collect the Holder object with unit and name
.collect( // collect the map
Collectors.groupingBy(t -> t.key, // define the grouping
Collector.of(
HashSet::new, // accumulator.
(set, el) -> set.add(el.value),// how to add data.
(left, right) -> { // for aggregation.
left.addAll(right);
return left;
}
)
)
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question