问:下面的代码输出结构是什么?
提示:Person类重写了toString方法、equals方法和hashCode方法
HashSet<Person> hashSet = new HashSet<>();
Person p1 = new Person(1001, "AA");
Person p2 = new Person(1002, "BB");
hashSet.add(p1);
hashSet.add(p2);
p1.setName("CC");
hashSet.remove(p1);
System.out.println(hashSet);
hashSet.add(new Person(1001, "CC"));
System.out.println(hashSet);
hashSet.add(new Person(1001, "AA"));
System.out.println(hashSet);
答案是:
[Person{id=1002, name='BB'}, Person{id=1001, name='CC'}]
[Person{id=1002, name='BB'}, Person{id=1001, name='CC'}, Person{id=1001, name='CC'}]
[Person{id=1002, name='BB'}, Person{id=1001, name='CC'}, Person{id=1001, name='CC'}, Person{id=1001, name='AA'}]