合併地圖我在那裏每次迭代I'm創建一個新的地圖的Java 8中的迭代器
Map<String, List<String>>
現在我想在每一個合併迭代用新的最後發射地圖的iteraror。
如果我送物品的列表getMap
{"a","a","b"}
我希望收到地圖的
["a",{"foo:a", "foo:a"}, "b",{"foo:b"}]
一個我試圖用減少功能,但由於的putAll只有工作,如果我使用多重映射而不是地圖,不是一個好的選擇。
這裏我的代碼
public Map<String, List<String>> getMap(List<String> items){
return items().stream()
.map(item -> getNewMap(item) --> Return a Map<String, List<String>>
.reduce(new HashMap<>(), (o, p) -> {
o.putAll(p);
return o;
});
}
public Map<String, List<String>> getNewMap(String item){
Map<String, List<String>> map = new HashMap<>();
map.put(item, Arrays.asList("foo:" + item));
return map;
}
I'm找一個沒有詳細辦法做到這一點。
不要考慮接受Holger的解決方案,而不是我的,它是一個更清潔,更好。 – Tunaki