韩顺平hashset2练习作业

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);
    }
}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值