我试图存储HashMap
另一个HashMap
但第一次插入的值更改为第二次插入的值。
这是我的代码。
HashMap<String ,HashMap<Integer ,Integer>> map1=new HashMap<>();
HashMap<Integer ,Integer> map2=new HashMap<>();
map2.put(1,1);
map2.put(2,1);
map2.put(3,1);
map1.put("1", map2);
System.out.println("After Inserting first value "+map1.entrySet());
/* OutPut: After Inserting first value [1={1=1, 2=1, 3=1}]*/
map2.clear(); //i cleared map 2 values
map2.put(4,2);
map2.put(5,2);
map2.put(6,2);
map1.put("2", map2);
System.out.println("After Inserting second value "+map1.entrySet());
/*output : After Inserting second value [2={4=2, 5=2, 6=2}, 1={4=2, 5=2, 6=2}]*/
1={1=1, 2=1, 3=1}]
在插入第二个“键,值”后第一次得到输出时,[2={4=2, 5=2, 6=2}, 1={4=2, 5=2, 6=2}]
我将键“1”值更改为键“2”。