public class HashSetDemo {
public static void main(String[] args) {
//创建集合
HashSet hash = new HashSet();
//创建学生对对象
Student s1 = new Student("one",1);
Student s2 = new Student("two",2);
Student s3 = new Student("two",2);
Student s4 = new Student("three",3);
Student s5 = new Student("four",4);
Student s6 = new Student("four",4);
Student s7 = new Student("five",5);
//添加
hash.add(s1);
hash.add(s2);
hash.add(s3);
hash.add(s4);
hash.add(s5);
hash.add(s6);
hash.add(s7);
//遍历
for(Student s : hash)
{
System.out.println(s.getAge() + s.getName());
//不是说唯一性吗?为什么打印出来发现相同的元素还是有的呢?
//原因是因为低沉用hashcode和equals方法进行比较的
//而对象却没有重写父类Object的hashcode和equals方法!所以当然不同了!
}
}
}