@Test
public void cloneMapTest() throws Exception {
HashMap<User,Integer> map=new HashMap<User,Integer>();
map.put(new User("wang", 11),1);
map.put(new User("hao", 12),2);
map.put(new User("wan", 13), 3);
HashMap<User,Integer> map2=(HashMap<User, Integer>) map.clone();
map2.remove(new User("hao",12));
for(Entry<User, Integer> entry:map.entrySet()){
System.out.println(entry.getKey().getAge());
System.out.println(entry.getKey().getName());
}
}
JDK里说 HashMap中的clone方法是浅复制,那map2里的修改会影响map。但结果却是
12
hao
13
wan
11
wang
没有把那个new User("hao",12) 给去掉,求大神解答!!
我知道map是深复制了一个map2,但map中的table还是引用。这个不能理解