同じ型を含む 2 つの HashMap オブジェクトを結合するにはどうすればよいですか? 質問する

同じ型を含む 2 つの HashMap オブジェクトを結合するにはどうすればよいですか? 質問する

次のように定義された2 つのオブジェクトがありますHashMap

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

3つ目のオブジェクトもありますHashMap

HashMap<String, Integer> map3;

map1map2を にマージするにはどうすればいいですかmap3?

ベストアンサー1

map3 = new HashMap<>();

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

おすすめ記事