我有几个类,如下
class Pojo {
List<Item> items;
}
class Item {
T key1;
List<SubItem> subItems;
}
class SubItem {
V key2;
Object otherAttribute1;
}
Run Code Online (Sandbox Code Playgroud)
我想根据key1
每个聚合聚合项目,子项目应按key2
以下方式聚合:
Map<T, Map<V, List<Subitem>>
Run Code Online (Sandbox Code Playgroud)
Java 8 Collectors.groupingBy
嵌套有什么可能吗?
我正在尝试一些东西并且中途停留
pojo.getItems()
.stream()
.collect(
Collectors.groupingBy(Item::getKey1, /* How to group by here SubItem::getKey2*/)
);
Run Code Online (Sandbox Code Playgroud)
注意:这与级联不同groupingBy
,后者根据此处讨论的同一对象中的字段进行多级聚合