hashset实现不添加员工姓名和生日相同的数据

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值