如何组合包含相同类型的两个 HashMap 对象? [英] How can I combine two HashMap objects containing the same types?

查看:34
本文介绍了如何组合包含相同类型的两个 HashMap 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two HashMap objects defined like so:

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

我还有第三个 HashMap 对象:

I also have a third HashMap object:

HashMap<String, Integer> map3;

如何将 map1map2 合并到 map3 中?

How can I merge map1 and map2 together into map3?

推荐答案

map3 = new HashMap<>();

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

这篇关于如何组合包含相同类型的两个 HashMap 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆