Java HashSet存储学生对象并遍历

HashSet存储学生对象并遍历

  • 需求:
    创建一个存储学生对象的集合,存储多个学生对象,使用程序实现在控制台遍历该集合
  • 要求:
    学生对象的成员变量值相同,我们就认为是同一个对象
public class Student {
	private String name;
	private int age;

	public Student() {
	}

	public Student(String name, int age) {
		super();
		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;
	}

		//重写hashcode和equals
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
public class HashSetTest {
	public static void main(String[] args) {
		HashSet<Student> hs = new HashSet<>();
		Student s1 = new Student("小明", 18);
		Student s2 = new Student("小蓝", 22);
		Student s3 = new Student("小红", 20);
		Student s4 = new Student("小明", 18);

		hs.add(s1);
		hs.add(s2);
		hs.add(s3);
		hs.add(s4);

		for (Student s : hs) {
			System.out.println(s.getName() + "," + s.getAge());
			/*
			 * 小蓝,22 小明,18 小红,20
			 */
		}
	}
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值