HashSet最佳实践
Set接口实现类——HashSet
定义一个Employee类,该类包含:private成员属性name,age。要求:
- 创建3个Employee放入HashSet中
- 当name和age的值相同时,认为是相同员工,不能添加到HashSet集合中
import java.util.HashSet;
import java.util.Objects;
public class HashSetExercise {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Employee("test1",1));
hashSet.add(new Employee("test2",2));
hashSet.add(new Employee("test1",1));
System.out.println("hashSet = " + hashSet);
}
}
//创建Employee对象
class Employee{
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
//如果name和age值相同,则返回相同的hash值
@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 &&
Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, 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 +
'}';
}
}

浙公网安备 33010602011771号