使用Java8StreamAPI,我怎样才能将Map扁平成Pair列表,其中左对值是映射键?
例子:如果给出地图
1 => {1, 2, 3}
2 => {2, 4}然后,所需的输出是五对的流:
(1,1) , (1,2) , (1,3) , (2,2) , (2,4)发布于 2015-08-24 19:01:57
List<Pair<String, String>> result =
map.entrySet()
.stream()
.flatMap(
entry -> entry.getValue()
.stream()
.map(string -> new Pair<>(entry.getKey(), string)))
.collect(Collectors.toList());https://stackoverflow.com/questions/32189147
复制相似问题