韩顺平 HashSet 课后练习 2
题目如下:
按照要求简单写了一下,将Employee和MyDate类都重写了equals方法和hashCode方法
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 + '\'' +
'}';
}
}
输出结果如下:
看了视频的弹幕,有小伙伴说可以只重写一次equals和hashCode方法,于是我就试了试发现真的可以。相应的话就需要给MyDate类增加set和get方法。
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 + '\'' +
'}';
}
}
发现也是可以的,并且输出结果相同。
还有小伙伴说可以用成员内部类和匿名内部类的,如果有用这两种方法的欢迎评论区贴出代码交流,主要我没想明白MyDate类如何运用内部类。可以一起交流共同探讨。