引用
public class Test { public static void main(String[] args) { /* * 将一个对象加入一个集合中,集合中的对象与原对象指向的是同一个引用 */ HashMap<String, Obj> map = new HashMap<String, Obj>(); Obj obj = new Obj("wanghui"); map.put("wanghui", obj); System.out.println(map.get("wanghui").getName());//wanghui obj.setName("wangqiang"); System.out.println(map.get("wanghui").getName());//wangqiang ArrayList<Obj> list = new ArrayList<>(); list.add(obj); System.out.println(list.get(0).getName());//wangqiang obj.setName("wanghui"); System.out.println(list.get(0).getName());//wanghui } } class Obj{ private String name; public Obj(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }