@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
public class Children extends Student {
public Children() {
}
public Children(String name, int age) {
super(name, age);
}
}
然后是将Student类和Children类的对象放入Set集合中,即使两个对象中的属性值完全一样,Set集合也不会对其去重
package test.GenericDemo;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetDemo {
public static void main(String[] args) {
Set set =