Han Shunping HashSet Exercise 2

Han Shunping HashSet Exercise 2

The topic is as follows:
Homework
I briefly wrote it according to the requirements, rewriting the equals method and the hashCode method for both the Employee and MyDate classes

package com.SetExercise;

import org.junit.Test;

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

/**
 * @author 心向阳光的天域
 * @date 2022/10/7 15:19
 */
public class Employee02 {
    
    
    @Test
    public void getPerson() {
    
    
        HashSet hashSet = new HashSet();
        hashSet.add(new Employ("小明", 10000.88d, new MyDate("1996", "01", "01")));
        // 姓名一样,出生年份不同
        hashSet.add(new Employ("小明", 10001.88d, new MyDate("1997", "01", "01")));
        // 出生年份和工资不同
        hashSet.add(new Employ("小明", 10001.88d, new MyDate("1998", "01", "01")));
        // 工资和第一个比不同(按照规则,这个应该不展示)
        hashSet.add(new Employ("小明", 10002.88d, new MyDate("1996", "01", "01")));

		// 重写了toString()方法,这里可以遍历一下,看看值
        System.out.println("size:" + hashSet.size());
        for (Object o : hashSet) {
    
    
            System.out.println(o);
        }
    }
}

class Employ {
    
    
    private String name;
    private double sal;
    private MyDate birthday;

    public Employ(String name, double sal, MyDate birthday) {
    
    
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employ employ = (Employ) o;
        return Objects.equals(name, employ.name) && Objects.equals(birthday, employ.birthday);
    }

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

    @Override
    public String toString() {
    
    
        return "Employ{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}

class MyDate {
    
    
    String year;
    String month;
    String day;

    public MyDate(String year, String month, String day) {
    
    
        this.year = year;
        this.month = month;
        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 Objects.equals(year, myDate.year) && Objects.equals(month, myDate.month) && Objects.equals(day, myDate.day);
    }

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

    @Override
    public String toString() {
    
    
        return "MyDate{" +
                "year='" + year + '\'' +
                ", month='" + month + '\'' +
                ", day='" + day + '\'' +
                '}';
    }
}


The output is as follows:
insert image description here

After watching the barrage of the video, some friends said that the equals and hashCode methods can be rewritten only once, so I tried it and found that it is really possible. Correspondingly, you need to add set and get methods to the MyDate class.

package com.SetExercise;

import org.junit.Test;

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

/**
 * @author 心向阳光的天域
 * @date 2022/10/7 15:19
 */
public class Employee02 {
    
    
    @Test
    public void getPerson() {
    
    
        HashSet hashSet = new HashSet();
        hashSet.add(new Employ("小明", 10000.88d, new MyDate("1996", "01", "01")));
        hashSet.add(new Employ("小明", 10001.88d, new MyDate("1997", "01", "01")));
        hashSet.add(new Employ("小明", 10001.88d, new MyDate("1998", "01", "01")));
        // 按照重复数据去重
        hashSet.add(new Employ("小明", 10002.88d, new MyDate("1996", "01", "01")));

        System.out.println("size:" + hashSet.size());
        for (Object o : hashSet) {
    
    
            System.out.println(o);
        }
    }
}

class Employ {
    
    
    private String name;
    private double sal;
    private MyDate birthday;

    public Employ(String name, double sal, MyDate birthday) {
    
    
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employ employ = (Employ) o;
        return Objects.equals(name, employ.name)
                // 这里直接重写一次equals方法即可
                && Objects.equals(birthday.getYear(), employ.birthday.getYear())
                && Objects.equals(birthday.getMonth(), employ.birthday.getMonth())
                && Objects.equals(birthday.getDay(), employ.birthday.getDay());
    }

    @Override
    public int hashCode() {
    
    
        // 这里直接重写一次hashCode方法即可
        return Objects.hash(name, birthday.year, birthday.month, birthday.day);
    }

    @Override
    public String toString() {
    
    
        return "Employ{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}

class MyDate {
    
    
    String year;
    String month;
    String day;

    public MyDate(String year, String month, String day) {
    
    
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public String getYear() {
    
    
        return year;
    }

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

    public String getMonth() {
    
    
        return month;
    }

    public void setMonth(String month) {
    
    
        this.month = month;
    }

    public String getDay() {
    
    
        return day;
    }

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

    @Override
    public String toString() {
    
    
        return "MyDate{" +
                "year='" + year + '\'' +
                ", month='" + month + '\'' +
                ", day='" + day + '\'' +
                '}';
    }
}

Discovery works as well, and the output is the same.

Some friends also said that member internal classes and anonymous internal classes can be used. If you use these two methods, you are welcome to post code in the comment area for exchange. Mainly, I didn’t want to understand how the MyDate class uses internal classes. We can communicate and discuss together.

Guess you like

Origin blog.csdn.net/sinat_38316216/article/details/127195109#comments_27627529