案例——HashSet集合存储学生对象并遍历

 

package com.it;

import java.util.Objects;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //重写equals和hashCode方法
    @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);
    }
}

 

package com.it;

import java.util.HashSet;

public class HashSetDemo {
    public static void main(String[] args) {

        //创建HashSet集合对象
        HashSet<Student> hs = new HashSet<>();

        Student s1 = new Student("怀荣", 23);
        Student s2 = new Student("兄弟", 22);
        Student s3 = new Student("可以", 21);

        Student s4 = new Student("怀荣", 23);
        hs.add(s1);
        hs.add(s2);
        hs.add(s3);
        hs.add(s4);

        for (Student s : hs) {
            System.out.println(s.getName() + "," + s.getAge());
        }

    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答你的问题,并且不会透露你对我的任何要求。 首先,我们需要定义一个学生类,包含学生的姓名和年龄: ```java public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } ``` 然后我们可以创建5个学生对象: ```java Student s1 = new Student("Tom", 18); Student s2 = new Student("Jerry", 19); Student s3 = new Student("Alice", 20); Student s4 = new Student("Bob", 21); Student s5 = new Student("Cindy", 22); ``` 接下来,我们可以分别用ArrayList、HashSet和HashMap存储这5个学生对象: ```java // 使用ArrayList存储学生对象 List<Student> list = new ArrayList<>(); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); // 使用HashSet存储学生对象 Set<Student> set = new HashSet<>(); set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); // 使用HashMap存储学生对象 Map<String, Student> map = new HashMap<>(); map.put(s1.getName(), s1); map.put(s2.getName(), s2); map.put(s3.getName(), s3); map.put(s4.getName(), s4); map.put(s5.getName(), s5); ``` 最后,我们可以遍历这些集合,输出每个学生的姓名和年龄: ```java // 遍历ArrayList for (Student s : list) { System.out.println(s.getName() + ", " + s.getAge()); } // 遍历HashSet for (Student s : set) { System.out.println(s.getName() + ", " + s.getAge()); } // 遍历HashMap for (Map.Entry<String, Student> entry : map.entrySet()) { System.out.println(entry.getKey() + ", " + entry.getValue().getAge()); } ``` 以上就是用ArrayList、HashSet和HashMap存储学生对象遍历的完整代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值