嵌套流java 8

嵌套流java 8,java,java-8,java-stream,Java,Java 8,Java Stream,正在尝试为此使用Java8流 HashMap<String,Games> groupGames = new HashMap<String,Games> (); for(Groups group: groups){ for(int groupId : group.getGroupId){ groupGames.put(groupId, group.getGame()); } } 我很难把游戏中的每个groupId都放到hashma

正在尝试为此使用Java8流

 HashMap<String,Games> groupGames = new HashMap<String,Games> ();
 for(Groups group: groups){
    for(int groupId : group.getGroupId){
          groupGames.put(groupId, group.getGame());
    }
 }
我很难把游戏中的每个groupId都放到hashmap中
我能够平展GroupID列表

groups.stream()
 groups.stream()
       .flatMap(x -> x.getGroupId().stream()
                    .map(y -> new AbstractMap.SimpleEntry<>(y, x.getGame())))
       .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (x, y) -> y));
.flatMap(x->x.getGroupId().stream()) .map(y->newAbstractMap.SimpleEntry(y,x.getGame())) .collect(Collectors.toMap(条目::getKey,条目::getValue,(x,y)->y));

如果您知道不会有重复的,可以删除
(x,y)->y
中的最后一个参数;或者您可以提供一个您认为合适的合并函数

你试过什么?你的问题是什么,除了“为我写代码”?你在这个网站上试过搜索吗:我试过了,但我似乎做不到它的嵌套部分。。。这是我的东西。stream().map(groups->groups.getGroupIds()).flatMap(group->groups.stream()).collect(collector.toList());可以删除注释。在问题本身中加入相关细节。并告诉我们你尝试了什么,甚至参考了其他问答。请看看这个问题:如果我错了,请纠正我。但是链接的解决方案不处理重复的
groupId
,这可能是OP.
Map.entry(y,x.getGame())
?@Federicoperaltachaffner
,因为9
。。。
 groups.stream()
       .flatMap(x -> x.getGroupId().stream()
                    .map(y -> new AbstractMap.SimpleEntry<>(y, x.getGame())))
       .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (x, y) -> y));