java - 如何组合包含相同类型的两个 HashMap 对象?

标签 java hashmap

我有两个 HashMap 对象定义如下:

HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();

我还有第三个 HashMap 对象:

HashMap<String, Integer> map3;

如何将 map1map2 合并为 map3

最佳答案

map3 = new HashMap<>();

map3.putAll(map1);
map3.putAll(map2);

关于java - 如何组合包含相同类型的两个 HashMap 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4299728/

相关文章:

java - HashMap 和可见性

java - 如何在 Hibernate 中使用单独的队列填充实体的字段?

java - 求 1 的补码

java - Java Collections.sort(nodes) 使用什么类型?

java - 调用HashMap的 'dynamic'对象

android - Android 中 HashMap 的内存效率

C字典/ map

java - onServicesDiscovered 给出 'Method does not override method from its super class' 错误

java - 无限跨度 : Local cache only

java - 我应该如何实现这个 HashMap 的 equals 和 hashCode 方法来表示自动机状态?