Dar*_*arz 12 java lambda java-8 java-stream
我怎么能展平Stream的Map(同类型)s到一个Map用Java 8?
Map<String, Long> toMap(Stream<Map<String, Long>> stream) {
return stream. ???
}
Run Code Online (Sandbox Code Playgroud)
Era*_*ran 20
我的语法可能有点偏,但flatMap应该为你完成大部分工作:
Map<String, Long> toMap(Stream<Map<String, Long>> stream) {
return stream.flatMap (map -> map.entrySet().stream()) // this would create a flattened
// Stream of all the map entries
.collect(Collectors.toMap(e -> e.getKey(),
e -> e.getValue())); // this should collect
// them to a single map
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10864 次 |
| 最近记录: |