设置无法删除重复项

时间:2014-11-28 14:03:46

标签: java set

我有一个查询,即我已经开发了一个pojo,并且在该pojo中我已经定制了hashcode方法和equals方法,如下所示在名为Emp.java的pojo中,现在我的查询是我正在添加该类型的对象在集合中我也开发了seprately,rite现在我没有采用泛型的优势以后我将自定义它但是现在我在set中添加对象但是在设置重复对象的结果中显示,如理想情况下,必须显示独特的对象,请告诉我如何克服这个问题..

我的pojo是..

class Emp implements Comparable
{
      String name,job;
      int salary;
      public Emp(String n,String j,int sal)
      {
         name=n;
         job=j;
         salary=sal;
       }
      public void display()
      {
        System.out.println(name+"\t"+job+"\t"+salary);
       }
      public boolean equals(Object o)
      {
          Emp p=(Emp)o;
          return this.name.equals(p.name)&&this.job.equals(p.job)&&this.salary==p.salary;
       }
       public int hashCode()
       {
          return name.hashCode()+job.hashCode()+salary;
       }  
       public int compareTo(Object o)
       {
          Emp e=(Emp)o;
          return this.name.compareTo(e.name);
           //return this.job.compareTo(e.job);
          // return this.salary-e.salary;

        }
}

我的集合类是..

public class empset {

    public static void main(String args[])
    {
    Set s1=new HashSet();
    s1.add(new Emp("saral","coder",2300));
    s1.add(new Emp("wer","der",2560));
    s1.add(new Emp("Sachin","Programmer",24000));
    s1.add(new Emp("Sachin","Programmer",24000));


    System.out.println("There are "+s1.size()+" elements in the set.");
    System.out.println("Content of set are : ");

    Iterator itr=s1.iterator();
    while(itr.hasNext())
    {
      Emp e=(Emp)itr.next();
      System.out.print(e.hashCode()+"\t");   
      e.display();
    }   
    }

执行后,我得到的结果是......

There are 4 elements in the set.
Content of set are : 
15075785    wer der 2560
28921555    saral   coder   2300
9209934 Sachin  Programmer  24000
4766781 Sachin  Programmer  24000

但理想情况下,该集应删除重复项,因此我得到的结果是..

There are 3 elements in the set.
Content of set are : 
15075785    wer der 2560
28921555    saral   coder   2300
9209934 Sachin  Programmer  24000

因为sachin名称是重复的并且在集合中应该被视为一次

1 个答案:

答案 0 :(得分:1)

我尝试了你的代码,修复了一些明显的编译错误,并成功删除了第4个元素:

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Empset
{
    static class Emp implements Comparable
    {
        String name,job;
        int salary;
        public Emp(String n,String j,int sal)
        {
            name=n;
            job=j;
            salary=sal;
        }
        public void display()
        {
            System.out.println(name+"\t"+job+"\t"+salary);
        }
        public boolean equals(Object o)
        {
            Emp p=(Emp)o;
            return this.name.equals(p.name)&&this.job.equals(p.job)&&this.salary==p.salary;
        }
        public int hashCode()
        {
            return name.hashCode()+job.hashCode()+salary;
        }
        public int compareTo(Object o)
        {
            Emp e=(Emp)o;
            return this.name.compareTo(e.name);
        }
    }

    public static void main(String args[])
    {
        Set s1=new HashSet();
        s1.add(new Emp("saral","coder",2300));
        s1.add(new Emp("wer","der",2560));
        s1.add(new Emp("Sachin","Programmer",24000));
        s1.add(new Emp("Sachin","Programmer",24000));

        System.out.println("There are "+s1.size()+" elements in the set.");
        System.out.println("Content of set are : ");

        Iterator itr=s1.iterator();
        while(itr.hasNext())
        {
            Emp e=(Emp)itr.next();
            System.out.print(e.hashCode()+"\t");
            e.display();
        }
    }
}

<强>结果

There are 3 elements in the set.
Content of set are : 
199998062   Sachin  Programmer  24000
219509  wer der 2560
204044336   saral   coder   2300