[英]Returning the Key in a Map<String,List<Pair<Integer,String>>>
[英]Map<String,List<String>> to Pair<String,String>
使用Java 8 Stream API如何將一個Map
Pair
列表,其中左對值是映射鍵?
示例:如果給定地圖是
1 => {1, 2, 3}
2 => {2, 4}
然后所需的輸出是五對流:
(1,1) , (1,2) , (1,3) , (2,2) , (2,4)
List<Pair<String, String>> result =
map.entrySet()
.stream()
.flatMap(
entry -> entry.getValue()
.stream()
.map(string -> new Pair<>(entry.getKey(), string)))
.collect(Collectors.toList());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.