package collection_;
import java.util.HashSet;
import java.util.Objects;
public class hashset02 {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Emplyee("jkjk", 'n', new Emplyee.MyDate("2000", "2")));
hashSet.add(new Emplyee("jkjk", 'n', new Emplyee.MyDate("2000", "2")));
hashSet.add(new Emplyee("jkjk", 'n', new Emplyee.MyDate("2000", "2")));
System.out.println("hashSet=" + hashSet);
}
}
class Emplyee {// 定义类
private String name;
private char sel;
private MyDate birthday;
public Emplyee(String name, char sel, MyDate birthday) {
this.name = name;
this.sel = sel;this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSel() {
return sel;
}
public void setSel(char sel) {
this.sel = sel;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
static class MyDate {// 定义静态内部类
private String year;
private String moth;
public MyDate(String year, String moth) {
this.year = year;
this.moth = moth;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getMoth() {
return moth;
}
public void setMoth(String moth) {
this.moth = moth;
}
@Override
public String toString() {
return "MyDate{" +
"year='" + year + '\'' +
", moth='" + moth + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyDate myDate = (MyDate) o;
return Objects.equals(year, myDate.year) && Objects.equals(moth, myDate.moth);
}
@Override
public int hashCode() {
return Objects.hash(year, moth);
}
}
@Override
public String toString() {
return "Emplyee{" +
"name='" + name + '\'' +
", sel=" + sel +
", birthday=" + birthday +
'}';
}
@Override
//判断是否重复
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Emplyee emplyee = (Emplyee) o;
return sel == emplyee.sel && Objects.equals(name, emplyee.name) && Objects.equals(birthday, emplyee.birthday);
}
@Override
public int hashCode() {
return Objects.hash(name, sel, birthday);
}
}
hashset实现不添加员工姓名和生日相同的数据
最新推荐文章于 2024-07-22 14:15:06 发布