我已经在Employee类里面覆写了hashcode() 和equals()方法,我向ArrayList数组里面添加了两次e1这个对象 按道理来说输出结果里面不应重复的啊?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
class Dept{
private int dnum;
private String dname;
private String location;
private List emps;
public Dept(){
this.emps=new ArrayList();
}
public Dept(int dnum, String dname, String location) {
this();
this.dnum = dnum;
this.dname = dname;
this.location = location;
}
public void setEmp(List emps){
this.emps=emps;
}
public List getEmp(){
return this.emps;
}
public String toString(){
return "部门编号"+dnum+",部门名称"+dname+",位置"+location;
}
}
class Employee{
private int pnum;
private String pname;
private String position;
private double basicSalary;
private double bonus;
private Employee Manager;
private Dept dept;
public Employee(){
}
public Employee(int pnum, String pname, String position, double basicSalary, double bonus) {
super();
this.pnum = pnum;
this.pname = pname;
this.position = position;
this.basicSalary = basicSalary;
this.bonus = bonus;
}
public void setManager(Employee e){
this.Manager=e;
}
public Employee getManager(){
return this.Manager;
}
public void setDept(Dept d){
this.dept=d;
}
public Dept getDept(){
return this.dept;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((Manager == null) ? 0 : Manager.hashCode());
long temp;
temp = Double.doubleToLongBits(basicSalary);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(bonus);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((pname == null) ? 0 : pname.hashCode());
result = prime * result + pnum;
result = prime * result + ((position == null) ? 0 : position.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (Manager == null) {
if (other.Manager != null)
return false;
} else if (!Manager.equals(other.Manager))
return false;
if (Double.doubleToLongBits(basicSalary) != Double.doubleToLongBits(other.basicSalary))
return false;
if (Double.doubleToLongBits(bonus) != Double.doubleToLongBits(other.bonus))
return false;
if (pname == null) {
if (other.pname != null)
return false;
} else if (!pname.equals(other.pname))
return false;
if (pnum != other.pnum)
return false;
if (position == null) {
if (other.position != null)
return false;
} else if (!position.equals(other.position))
return false;
return true;
}
public String toString(){
return "雇员编号"+pnum+",姓名"+pname+",职位"+position+",基本工资"+basicSalary+",奖金"+bonus;
}
}
public class TestDemo{
public static void main(String[] args) {
Dept d=new Dept(01,"Accounting", "NEWYORK");
Employee e1=new Employee(001, "peter", "会计", 6000.0, 1000.0);
Employee e2=new Employee(002, "jane", "程序员", 7000.0, 2000.0);
Employee e3=new Employee(003, "maria", "经理", 9000.0, 3000.0);
d.getEmp().add(e1);
d.getEmp().add(e1); //这里添加了两次e1
d.getEmp().add(e2);
d.getEmp().add(e3);
e1.setDept(d);
e2.setDept(d);
e3.setDept(d);
e1.setManager(e3);
e2.setManager(e3);
System.out.println(d);
Iterator itr=d.getEmp().iterator();
while(itr.hasNext()){
Employee emp=itr.next();
System.out.println(emp);
if(emp.getManager()!=null){
System.out.println("\t"+emp.getManager());
}
}
}
}