/** R - flat Row P - Parent class C - Child class */ public List

groupFlatRows(Collection rows , Function mapRowToParent , Function mapRowToChild , BiConsumer> setParentChildren , Comparator

sortParents) { // преобразовываем Collection

в Map> return rows.stream() .collect(groupingBy(mapRowToParent , mapping(mapRowToChild , Collectors.toList()))) // преобразовываем Map> в List

, устанавливая дочерний List в каждый P .entrySet().stream() .map(entry -> { P parent = entry.getKey(); List children = entry.getValue(); setParentChildren.accept(parent, children); return parent; }) // Stream

.sorted(sortParents) .collect(Collectors.toList()); }