如何组合两个包含相同类型的 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

原文由 Mavin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 511
1 个回答
map3 = new HashMap<>();

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

原文由 a_horse_with_no_name 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题