HashSet练习
下图为重写equals方法的位置,alt+insert
package com.example.hspedu;
import java.util.HashSet;
import java.util.Objects;
public class exercise01 {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Employee("tom",12));
hashSet.add(new Employee("milan",12));
hashSet.add(new Employee("tom",12));
System.out.println("hashSet = " + hashSet);
}
}
class Employee{
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return age == employee.age &&
name.equals(employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
最后结果
下面是一道附加题,答案是总计写的,如果大家觉得有不足之处或者其他的思路,欢迎指出
package com.example.hspedu;
import java.util.HashSet;
import java.util.Objects;
public class exercise02 {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Employee2("jack",3000,new MyDate(1999,12,20)));
hashSet.add(new Employee2("tom",5000,new MyDate(1998,6,20)));
hashSet.add(new Employee2("jack",3000,new MyDate(1999,12,20)));
System.out.println("hashSet = " + hashSet);
}
}
class Employee2{
private String name;
private double sal;
private MyDate birthday;
public Employee2(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;
Employee2 employee2 = (Employee2) o;
return Objects.equals(name, employee2.name) &&
Objects.equals(birthday, employee2.birthday);
}
@Override
public int hashCode() {
return Objects.hash(name, birthday);
}
@Override
public String toString() {
return "Employee2{" +
"name='" + name + '\'' +
", sal=" + sal +
", birthday=" + birthday +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
}
class MyDate{
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", 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 &&
month == myDate.month &&
day == myDate.day;
}
@Override
public int hashCode() {
return Objects.hash(year, month, day);
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
最后结果