Java 8流图<;长,列表<;MyClass>&燃气轮机;映射<;长,设置<;长期>&燃气轮机;

Java 8流图<;长,列表<;MyClass>&燃气轮机;映射<;长,设置<;长期>&燃气轮机;,java,java-8,java-stream,Java,Java 8,Java Stream,我想(使用Java 8流)在映射中转换映射,其中集合表示列表中每个MyClass的id 我试过: myFirstMap.entrySet().stream() .map(e -> e.getValue().stream() .map(MyClass::getId) .collect(Collectors.toSet())) 但是我看不出如何得到结果。您正在映射地图的实例。Entry到Set实例,这意味着失去原始地图关键点的轨迹,从而无法

我想(使用Java 8流)在
映射
中转换
映射
,其中
集合
表示
列表
中每个
MyClass
id

我试过:

myFirstMap.entrySet().stream()
      .map(e -> e.getValue().stream()
          .map(MyClass::getId)
          .collect(Collectors.toSet()))

但是我看不出如何得到结果。

您正在映射
地图的实例。Entry
Set
实例,这意味着失去原始地图关键点的轨迹,从而无法将它们收集到具有相同关键点的新地图中

第一个选项是将
map.Entry
实例映射到
map.Entry
实例,然后将条目收集到新的映射:

Map<Long, Set<Long>> result=
    myFirstMap.entrySet().stream()
        .map(e -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(),
               e.getValue().stream().map(MyClass::getId).collect(Collectors.toSet())))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

这样,您可以避免创建新的
Map.Entry
实例,并获得更简洁的代码,灵活性不强,因为您无法在这两者之间链接其他流操作。

您正在将
Map.Entry
的实例映射到
Set
实例,这意味着失去原始地图关键点的轨迹,从而无法将它们收集到具有相同关键点的新地图中

第一个选项是将
map.Entry
实例映射到
map.Entry
实例,然后将条目收集到新的映射:

Map<Long, Set<Long>> result=
    myFirstMap.entrySet().stream()
        .map(e -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(),
               e.getValue().stream().map(MyClass::getId).collect(Collectors.toSet())))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

通过这种方式,您可以避免创建新的
Map.Entry
实例并获得更简洁的代码,但是,由于无法在两者之间链接额外的流操作,因此灵活性较低。

另一种解决方案是使用
Map.forEach()
,而不需要外部
流的开销

Map<Long,Set<Long>> result = new HashMap<>();
myFirstMap.forEach((k,v) -> 
    result.put(k, v.stream()
        .map(MyClass::getId)
        .collect(Collectors.toSet())));
Map result=newhashmap();
myFirstMap.forEach((k,v)->
结果.put(k,v.stream()
.map(MyClass::getId)
.collect(Collectors.toSet());
这其实只是一种方便的方法:

Map<Long,Set<Long>> result = new HashMap<>();
for (Map.Entry<Long, List<MyClass>> entry : myFirstMap.entrySet()) {
    result.put(entry.getKey(), entry.getValue().stream()
        .map(MyClass::getId)
        .collect(Collectors.toSet()));
}
Map result=newhashmap();
对于(Map.Entry:myFirstMap.entrySet()){
result.put(entry.getKey(),entry.getValue().stream())
.map(MyClass::getId)
.collect(Collectors.toSet());
}

另一个没有外部
流开销的解决方案是使用
Map.forEach()
如下:

Map<Long,Set<Long>> result = new HashMap<>();
myFirstMap.forEach((k,v) -> 
    result.put(k, v.stream()
        .map(MyClass::getId)
        .collect(Collectors.toSet())));
Map result=newhashmap();
myFirstMap.forEach((k,v)->
结果.put(k,v.stream()
.map(MyClass::getId)
.collect(Collectors.toSet());
这其实只是一种方便的方法:

Map<Long,Set<Long>> result = new HashMap<>();
for (Map.Entry<Long, List<MyClass>> entry : myFirstMap.entrySet()) {
    result.put(entry.getKey(), entry.getValue().stream()
        .map(MyClass::getId)
        .collect(Collectors.toSet()));
}
Map result=newhashmap();
对于(Map.Entry:myFirstMap.entrySet()){
result.put(entry.getKey(),entry.getValue().stream())
.map(MyClass::getId)
.collect(Collectors.toSet());
}

您的编辑将使已存在一年的答案无效。请不要这样做。您的编辑将使已存在一年的答案无效。请不要那样做。