import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* @Author WangQi
* @Date 2021/3/22 23:28
* @Description
*/
@SuppressWarnings({"all"})
public class TestHashset2 {
public static void main(String[] args) {
Set set = new HashSet();
set.add(new Employee("jack",2000,new Mydate(2011,12,1)));
set.add(new Employee("lucy",2000,new Mydate(2011,12,1)));
set.add(new Employee("lucy",2000,new Mydate(2012,12,1)));
set.add(new Employee("jack",2000,new Mydate(2011,12,1)));
System.out.println(set);
}
}
class Employee{
private String name;
private Integer sal;
private Mydate birthday;
public Employee(String name, Integer sal, Mydate birthday) {
this.name = name;
this.sal = sal;
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSal() {
return sal;
}
public void setSal(Integer sal) {
this.sal = sal;
}
public Mydate getBirthday() {
return birthday;
}
public void setBirthday(Mydate birthday) {
this.birthday = birthday;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return name.equals(employee.name) &&
birthday.equals(employee.birthday);
}
@Override
public int hashCode() {
return Objects.hash(name, birthday);
}
}
class Mydate{
private Integer year;
private Integer month;
private Integer day;
public Mydate(Integer year, Integer month, Integer day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return year + "-" + month + "-" + day;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Integer getMonth() {
return month;
}
public void setMonth(Integer month) {
this.month = month;
}
public Integer getDay() {
return day;
}
public void setDay(Integer day) {
this.day = day;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mydate mydate = (Mydate) o;
return year.equals(mydate.year) &&
month.equals(mydate.month) &&
day.equals(mydate.day);
}
@Override
public int hashCode() {
return Objects.hash(year, month, day);
}
}
韩顺平hashset2练习作业
最新推荐文章于 2023-02-18 11:26:07 发布