我有两个HashMap对象,定义如下:
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
我还有第三个HashMap对象:
HashMap<String, Integer> map3;
如何将map1和map2合并为map3?
我有两个HashMap对象,定义如下:
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
我还有第三个HashMap对象:
HashMap<String, Integer> map3;
如何将map1和map2合并为map3?
当前回答
使用Java 8 Stream API的一行程序:
map3 = Stream.of(map1, map2).flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue))
该方法的好处之一是能够传递一个merge函数,该函数将处理具有相同键的值,例如:
map3 = Stream.of(map1, map2).flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, Math::max))
其他回答
你可以对其他类型使用Collection.addAll(),例如List, Set等。对于Map,您可以使用putAll。
您可以使用- addAll方法
http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html
但是总会有这样的问题,如果你的两个哈希映射有相同的键,那么它会用第二个哈希映射的键值覆盖第一个哈希映射的键值。
为了安全起见-更改键值-您可以在键上使用前缀或后缀-(第一个哈希映射使用不同的前缀/后缀,第二个哈希映射使用不同的前缀/后缀)
使用Java 8 Stream API的一行程序:
map3 = Stream.of(map1, map2).flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue))
该方法的好处之一是能够传递一个merge函数,该函数将处理具有相同键的值,例如:
map3 = Stream.of(map1, map2).flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, Math::max))
HashMap有一个putAll方法。
http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html
用于合并两个映射的Java 8替代一行程序:
defaultMap.forEach((k, v) -> destMap.putIfAbsent(k, v));
方法参考也一样:
defaultMap.forEach(destMap::putIfAbsent);
或原始地图解与第三个地图的幂分量:
Map<String, Integer> map3 = new HashMap<String, Integer>(map2);
map1.forEach(map3::putIfAbsent);
下面是一个用Guava将两个映射合并为快速不可变映射的方法,它可以进行最少的中间复制操作:
ImmutableMap.Builder<String, Integer> builder = ImmutableMap.<String, Integer>builder();
builder.putAll(map1);
map2.forEach((k, v) -> {if (!map1.containsKey(k)) builder.put(k, v);});
ImmutableMap<String, Integer> map3 = builder.build();
请参见使用Java 8合并两个映射,了解需要使用映射函数组合两个映射中的值的情况。