java集合-- HashSet小练习

HashSet小练习

Employee类有name sal birthday(MyDate[year,moth,day]) 三个属性你

  • 如果name+birthday相同判断为统一个对象

注意点

  • birthday的不同取决于MyDate的不同,所以要先重写MyDate的hashcode()和equals()方法
    在这里插入图片描述
package cn.usts.edu.hashSet;

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

/**
 * @author :fly
 * @description: Employee类有name sal birthday(MyDate[year,moth,day]) 三个属性你
 *                 如果name+birthday相同判断为统一个对象
 *                 
 *               注意点:
 *                     birthday的不同取决于MyDate的不同,所以要先重写MyDate的hashcode()和equals()方法
 *                 
 * @date :2021/11/2 15:24
 */
public class Employee {
    
    
    private String name;
    private float sal;
    private MyDate birthday;

    public static void main(String[] args) {
    
    
        Employee jack = new Employee("jack", 4500.22f, new MyDate(1999, 10, 5));
        Employee jack1 = new Employee("jack", 5500.22f, new MyDate(1999, 10, 5));// sal 不同
        Employee jack2 = new Employee("jack", 5500.22f, new MyDate(1999, 10, 6));// day不同
        Employee jack3 = new Employee("jack", 5500.22f, new MyDate(1999, 11, 5));// month不同
        Employee jack4 = new Employee("jack", 3500.22f, new MyDate(1998, 10, 5));// year不同

        Employee luck = new Employee("luck", 5500.22f, new MyDate(1999, 10, 5));
        Employee luck2 = new Employee("luck2", 5500.22f, new MyDate(1999, 10, 5));

        HashSet set = new HashSet();
        set.add(jack);
        set.add(jack1);
        set.add(jack2);
        set.add(jack3);
        set.add(jack4);
        set.add(luck);
        set.add(luck2);
        set.forEach(obj-> System.out.println(obj));

    }


    public Employee() {
    
    
    }

    public Employee(String name, float sal, MyDate birthday) {
    
    
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
    
    
        return name;
    }

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

    public float getSal() {
    
    
        return sal;
    }

    public void setSal(float sal) {
    
    
        this.sal = sal;
    }

    public MyDate getBirthday() {
    
    
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
    
    
        this.birthday = birthday;
    }

    @Override
    public String toString() {
    
    
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", date=" + 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 Objects.equals(name, employee.name) && Objects.equals(birthday, employee.birthday);
    }

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

class MyDate{
    
    
    private int year;
    private int moth;
    private int day;

    public MyDate(int year, int moth, int day) {
    
    
        this.year = year;
        this.moth = moth;
        this.day = day;
    }

    public MyDate() {
    
    
    }

    public int getYear() {
    
    
        return year;
    }

    public void setYear(int year) {
    
    
        this.year = year;
    }

    public int getMoth() {
    
    
        return moth;
    }

    public void setMoth(int moth) {
    
    
        this.moth = moth;
    }

    public int getDay() {
    
    
        return day;
    }

    public void setDay(int day) {
    
    
        this.day = day;
    }


    @Override
    public String toString() {
    
    
        return "MyDate{" +
                "year=" + year +
                ", moth=" + moth +
                ", 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 == myDate.year && moth == myDate.moth && day == myDate.day;
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(year, moth, day);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43619461/article/details/121101969