hashset存储对象,以及如何对相同对象不进行储存

package haxi;

import java.util.HashSet;

public class Hashset {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		HashSet<Student> s1 = new HashSet<Student>();
		Student student1 = new Student("yyz", 18);
		Student student2 = new Student("yyzz", 18);
		Student student3 = new Student("yyzzz", 18);
		Student student4 = new Student("yyz", 18);

		s1.add(student1);
		s1.add(student2);
		s1.add(student3);

		for (Student string : s1) {
			Student s2 = string;
			System.out.println(s2.getName() + "," + s2.getAge());

		}
		System.out.println("此时加入重复对象yyz 18再次输出--------");
		s1.add(student4);
		for (Student string : s1) {
			Student s2 = string;
			System.out.println(s2.getName() + "," + s2.getAge());
//出现重复元素需重写原方法  放到下一段
		}
	}

}

class Student {
	String name;
	int 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;
	}

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

输出如下且相同对象可以储存

yyz,18
yyzz,18
yyzzz,18
此时加入重复对象yyz 18再次输出--------
yyz,18
yyzz,18
yyzzz,18
yyz,18

接下看重写后将不能存入相同对象(看起来

package haxi;

import java.util.HashSet;
import java.util.Objects;

public class Hashset {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		HashSet<Student> s1 = new HashSet<Student>();
		Student student1 = new Student("yyz", 18);
		Student student2 = new Student("yyzz", 18);
		Student student3 = new Student("yyzzz", 18);
		Student student4 = new Student("yyz", 18);

		s1.add(student1);
		s1.add(student2);
		s1.add(student3);

		for (Student string : s1) {
			Student s2 = string;
			System.out.println(s2.getName() + "," + s2.getAge());

		}
		System.out.println("此时加入重复对象yyz 18再次输出--------");
		s1.add(student4);
		for (Student string : s1) {
			Student s2 = string;
			System.out.println(s2.getName() + "," + s2.getAge());

		}
	}

}

class Student {
	String name;
	int age;

	public String getName() {
		return name;
	}

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

	@Override
	public int hashCode() {
		return Objects.hash(age, name);
	}

	@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;
		return age == other.age && Objects.equals(name, other.name);
	}

	public int getAge() {
		return age;
	}

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

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

输出如下

yyzz,18
yyz,18
yyzzz,18
此时加入重复对象yyz 18再次输出--------
yyzz,18
yyz,18
yyzzz,18

重复对象未储存

仅记录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值