qq_21532293
昏黄烛火轻摇晃
采纳率61.5%
2017-07-31 03:45 阅读 1.1k
已采纳

JAVA 类集数据重复问题 新手求解答

20

我已经在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());
}
}
}
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    Himly_Zhang Himly_Zhang 2017-07-31 04:36

    Set set = new HashSet(你的arrlist); 你的list.clear();你的list.addAll(set);
    这样可以打到通过重写equls去重复的目的。
    List 就是普通集合,装对象的没去重复功能。

    点赞 评论 复制链接分享
  • u011315960 珠穆朗玛小王子 2017-07-31 04:24

    ArrayLIst表示的是队列,内部并没有判断是否重复的机制,仅仅是队列而已,如果你需要不允许重复的话,应该使用HashSet,这些是基础知识,应该再好好的复习一下

    点赞 评论 复制链接分享

相关推荐