Java收集返回顶级项的嵌套映射流

Java收集返回顶级项的嵌套映射流,java,java-stream,collectors,Java,Java Stream,Collectors,我有以下型号: class Item { String name; ... List<SubItem> subItems; } class SubItem { String name; ... List<String> ids; } 目标是返回映射,其中: 键:id在子项中。id 值:AdditionalParams用于此id 我假设ID是唯一的,所以在向映射添加元素时不会有任何冲突 我的解决方案 我尝试将flatM

我有以下型号:

class Item {
    String name;
    ...
    List<SubItem> subItems;
}

class SubItem {
    String name;
    ...
    List<String> ids;
}
目标是返回
映射
,其中:

  • 键:
    id
    子项中。id
  • 值:
    AdditionalParams
    用于此
    id
我假设ID是唯一的,所以在向映射添加元素时不会有任何冲突

我的解决方案

我尝试将
flatMap
收集器一起使用。toMap

List<Item> allItems = ...
Map<String, AdditionalParams> result = allItems
    .stream()
    .flatMap(i -> i.getSubItems()
                   .stream()
                   .map(si -> si.getIds()
                                .stream()
                                .collect(
                                    Collectors.toMap(
                                        Function.identity(),
                                        id -> new AdditionalParams(i.getName(), si.getName())
                                    )
                                )
                   )
    )  // Stream< Map<String, AdditionalParams> >
    .collect(
        Collectors.toMap(m -> m.getKey(), m -> m.getValue())
    )
列出所有项=。。。
映射结果=所有项
.stream()
.flatMap(i->i.getSubItems()
.stream()
.map(si->si.getIds()
.stream()
.收集(
汤姆(
Function.identity(),
id->新的附加参数(i.getName(),si.getName())
)
)
)
)//流
.收集(
Collectors.toMap(m->m.getKey(),m->m.getValue())
)
问题是最后一行(
.collect(Collectors.toMap(…
)不起作用(或者我做错了)。我试图遵循这个答案,但也无法使它起作用


我的方法有什么错误?我如何获得类型为
Map
的结果?

您可以做些什么来修复这一问题,即将中间状态映射到映射的条目,然后收集它们。这将像--

Map结果=allItems
.stream()
.flatMap(项->项.getSubItems().stream())
.flatMap(子项->子项.getId()
.stream()
.map(id->new AbstractMap.SimpleEntry(
id,新的附加参数(item.getName(),
子项.getName()()()))
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey、,
SimpleEntry::getValue);
List<Item> allItems = ...
Map<String, AdditionalParams> result = allItems
    .stream()
    .flatMap(i -> i.getSubItems()
                   .stream()
                   .map(si -> si.getIds()
                                .stream()
                                .collect(
                                    Collectors.toMap(
                                        Function.identity(),
                                        id -> new AdditionalParams(i.getName(), si.getName())
                                    )
                                )
                   )
    )  // Stream< Map<String, AdditionalParams> >
    .collect(
        Collectors.toMap(m -> m.getKey(), m -> m.getValue())
    )
Map<String, AdditionalParams> result = allItems
        .stream()
        .flatMap(item -> item.getSubItems().stream()
                .flatMap(subItem -> subItem.getIds()
                        .stream()
                        .map(id -> new AbstractMap.SimpleEntry<>(
                                id, new AdditionalParams(item.getName(),
                                subItem.getName())))))
        .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey,
                AbstractMap.SimpleEntry::getValue));