ArrayList<Map<String, String>> result1
result1
就像
(1, a)
(2, a)
(3, b)
(4, e)
(5, e)
ArrayList<Map<String, String>> result2
result2
就像
(1,android)
(2,ios)
(3,android)
(4,android)
(5,ios)
我想将两张地图合并成一张这样的地图
(1, ( a, android))
(2, ( a, ios))
(3, ( b, android))
(4, (e, android))
(5, (e, ios))
如何做到这一点?
答案 0 :(得分:0)
对于您在此处指定的要求,您可以这样进行。
我正在遍历第一张地图的钥匙。并从所有地图中收集每个键的值,并将它们放在列表中。然后将列表放入生成的地图。
import java.util.*;
public class MergeMaps
{
public static void main(String[] args)
{
Map<String, String> map1 = new HashMap<>();
map1.put("1", "a");
map1.put("2", "a");
map1.put("3", "b");
map1.put("4", "e");
map1.put("5", "e");
Map<String, String> map2 = new HashMap<>();
map2.put("1", "android");
map2.put("2", "ios");
map2.put("3", "android");
map2.put("4", "android");
map2.put("5", "ios");
Set<String> keys = new HashSet<>();
keys.addAll(map1.keySet());
keys.addAll(map2.keySet());
Map<String, List<String>> mergedMap = new HashMap<>();
for (String key : keys)
{
List<String> list = new ArrayList<>();
list.add(map1.get(key));
list.add(map2.get(key));
mergedMap.put(key, list);
}
System.out.println(mergedMap);
}
}
输出将是:
{1=[a, android], 2=[a, ios], 3=[b, android], 4=[e, android], 5=[e, ios]}
答案 1 :(得分:0)
您可以将两个流与Stream.concat()
合并,并与Collectors.groupingBy()
和Collectors.mapping()
分组:
Map<String, String> first = Map.of("1", "a", "2", "a");
Map<String, String> second = Map.of("1", "android", "2", "ios");
Map<String, List<String>> result = Stream.concat(first.entrySet().stream(), second.entrySet().stream())
.collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toList())));
System.out.println(result);
将输出:
{1=[a, android], 2=[a, ios]}
答案 2 :(得分:0)
您也可以尝试这种方法:
Map<String, String> result1 = new HashMap<>();
// initialize result1 ...
Map<String, String> result2 = new HashMap<>();
// initialize result2 ...
Map<String, Map<String, String>> mergedResult = new HashMap<>();
最多Java 8
result1.forEach((k1, v1) ->
mergedResult.put(k1, new HashMap<String, String>() {{
put(v1, result2.get(k1));
}}));
Java 9或更高版本
result1.forEach((k1, v1) -> mergedResult.put(k1,
Map.of(v1, result2.get(k1))));
答案 3 :(得分:0)
这是得出结果的一种方法:
输入数据:
// The first list of data
List<Map<String, String>> list1 = new ArrayList<>();
list1.add(getMapData("1", "a"));
list1.add(getMapData("2", "a"));
list1.add(getMapData("3", "b"));
list1.add(getMapData("4", "e"));
list1.add(getMapData("5", "e"));
list1.add(getMapData("999", "x"));
System.out.println(list1);
数据1:[{1=a}, {2=a}, {3=b}, {4=e}, {5=e}, {999=x}]
// The second list of data
List<Map<String, String>> list2 = new ArrayList<>();
list2.add(getMapData("1", "android"));
list2.add(getMapData("2", "ios"));
list2.add(getMapData("3", "android"));
list2.add(getMapData("4", "android"));
list2.add(getMapData("5", "ios"));
list2.add(getMapData("888", "zzzzz"));
System.out.println(list2);
数据2:[{1=android}, {2=ios}, {3=android}, {4=android}, {5=ios}, {888=zzzzz}]
// utility method for creating test data
private static Map<String, String> getMapData(String k, String v) {
Map<String, String> m = new HashMap<>();
m.put(k, v);
return m;
}
输出存储到Map<String, List<String>>
:
Map<String, List<String>> result = new HashMap<>();
// process the first list
for (Map<String, String> m : list1) {
for (Map.Entry<String, String> entry : m.entrySet()) {
List<String> valueList = new ArrayList<>();
valueList.add(entry.getValue());
result.put(entry.getKey(), valueList);
}
}
// process the second list; merge with the first
for (Map<String, String> m : list2) {
for (Map.Entry<String, String> entry : m.entrySet()) {
String k = entry.getKey();
List<String> valueList = result.get(k);
if (valueList == null) {
valueList = new ArrayList<>();
}
valueList.add(entry.getValue());
result.put(k, valueList);
}
}
System.out.println(result);
结果:
{1=[a, android], 2=[a, ios], 3=[b, android], 4=[e, android], 5=[e, ios], 888=[zzzzz], 999=[x]}