一、创建HashSet对象
public class Test1 {
public static void main(String[] args) {
HashSet<Student> set = new HashSet<Student>();
set.add(new Student("100"));
set.add(new Student("100"));
}
}
二、创建学生类
public class Student {
private String id;
public Student(String id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
//obj是集合中的某个对象,因为集合中可能存的未必都是学生类,比如集合泛型为Object的时候,既可以存学生对象,还可以存其它对象
if(obj instanceof Student ){
Student stu = (Student)obj;
return this.id.equals