指针问题

public class People {
private String name;
private String age;
public People() {
}
}

  Map map1 = new HashMap<>();
   for (int i = 0; i < 5; i++) {
       map1.put(String.valueOf(i),new People(String.valueOf(i)));
   }
   Map map2 = new HashMap<>(map1);
   for(int i = 0;i<5;i++){
       People people = map2.get(String.valueOf(i));
       people.setName(i+"_也改变了map1里面people的值");
   }

Map1和Map2里面的value都指向了同一个People
所以在Map2里面取出People还是修改值,还是会导致Map1里面的值改变

你可能感兴趣的:(指针问题)