案例:HashSet集合存储学生对象并遍历
需求:创建一个存储学生对象的集合,存储多个学生对象,使用程序实现在控制台遍历该集合
要求:学生对象的成员变量值相同,我们就认为是同一个对象
思路:
1. 定义学生类
1. 创建HashSet集合对象
1. 创建学生对象
1. 把学生添加到集合
1. 遍历集合(增强for)
public static void main(String[] args) {
//2.创建HashSet集合对象
HashSet<Student> hs = new HashSet<Student>();
//3.创建学生对象
Student s1 = new Student("java",20);
Student s2 = new Student("HTML",30);
Student s3 = new Student("python",25);
Student s4 = new Student("python",25);
//4.把学生添加到集合
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
//5.遍历集合(增强for)
for (Student s: hs){
System.out.println(s.getName()+","+s.getAge());
}
}
-
在学生类中重写两个方法 (hashCode()和equals(Object o))
和getter/setter重写方法一致