62,625
社区成员
发帖
与我相关
我的任务
分享import java.util.*;
class employee{
private String id;
private String name;
private String age;
private String salary;
public employee(String id,String name,String age,String salary){
this.id=id;
this.name=name;
this.age=age;
this.salary=salary;
}
public String toString(){
return id+":"+name+","+age+","+salary+"";
}
public int hashcode(){
return id.hashCode();
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(!(obj instanceof employee)){
return false;
}
employee emp=(employee)obj;
boolean b=this.id.equals(emp.id);
return b;
}
}
public class information {
public static void main(String[] args) {
HashSet hs=new HashSet();
employee emp1=new employee("5","张三","25","3500");
employee emp2=new employee("2","李四","30","4000");
employee emp3=new employee("5","王五","40","5000");
hs.add(emp1);
hs.add(emp2);
hs.add(emp3);
System.out.print(hs);
}
}[2:李四,30,4000, 5:张三,25,3500, 5:王五,40,5000]这是输出后的结果,两个id都为5的对象都添加进去了,这是怎么回事?
@Override
public int hashCode() {
return super.hashCode();
}