一、子定义Person类在Set容器里的唯一值条件
1.自定义Person类
2.重写hashCode() 与 equals()函数
public class Person {
private String id;
private String name;
private int age;
public Person(){}
public Person(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
//确定自定义类型的hashCode的值
public int hashCode() {
System.out.println("hashcode");
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
//确定自定类型的比较方式: 这里是如果年龄、id、姓名都相等的话就是相等
//没有重写该方法的情况,系统默认使用Object的equals()方法 ,比较的是地址
public boolean equals(Object obj) {
System.out.println