Joh*_*xus 9 java linkedhashmap collectors
我该如何转换:
return this.subjects.entrySet()
.stream()
.collect(Collectors.toMap(e -> getArtistryCopy(e.getKey()), Map.Entry::getValue));
Run Code Online (Sandbox Code Playgroud)
要返回 LinkedHashMap 而不是地图?
如果您需要知道,this.subjects是一个LinkedHashMap<AbstractArtistries, ArrayList<AbstractCommand>>. AbstractArtistry 和 command 是我制作的两个自定义对象。我需要维持秩序。
getArtistryCopy() 返回 AbstractArtistry 的副本(这是关键)。
rge*_*man 14
您可使用的过载Collectors.toMap一个接受Supplier的Map。它还需要一个merge函数来解决重复键之间的冲突。
return this.subjects.entrySet()
.stream()
.collect(Collectors.toMap(e -> getArtistryCopy(e.getKey()),
Map.Entry::getValue,
(val1, val2) -> yourMergeResultHere,
LinkedHashMap::new));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5252 次 |
| 最近记录: |