关于去除重复元素的研究

时间:2012-08-01 17:17:21

标签: java collections

  

可能重复:
  Removing duplicates from the list with different ways

我正在进行一项研究并发现以下方法从列表中删除重复项...但请告知是否还有其他方法,因为我正在研究这个,我想在集合中探索其他方式将是一个伟大的..我对我的研究更感兴趣的是新的jdk 1.5有一些新的东西提供这种背景

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;

        }*/
} 

最后一堂课将是......

 class EmpListDemo
    {
          public static void main(String arg[])
          {
              List  list=new ArrayList ();
              list.add(new Emp("Ram","Trainer",34000));
              list.add(new Emp("Sachin","Programmer",24000));
              list.add(new Emp("Ram","Trainer",34000));
              list.add(new Emp("Priyanka","Manager",54000));
              list.add(1,new Emp("Ravi","Administrator",44000));
              list.add(new Emp("Anupam","Programmer",34000));
              list.add(new Emp("Priyanka","Manager",54000));
              list.add(new Emp("Sachin","Team Leader",54000));
              System.out.println("There are "+list.size()+" elements in the list.");



              System.out.println("Content of list are : ");
              ListIterator itr1=list.listIterator();
              while(itr1.hasNext())
              {
                Emp e=(Emp)itr1.next();  
                e.display();
              }           

//Removing duplicates from the list
          Set hs = new HashSet();
          hs.addAll(list);
          list.clear();
          list.addAll(hs);
          System.out.println("******************************");
          System.out.println("There are "+hs.size()+" elements in the set.");
          System.out.println("Contents after modification : ");
          Iterator itr=list.iterator();
          while(itr.hasNext())
          {
            Emp e=(Emp)itr.next();  
            e.display();
          }   




              }
    }

2 个答案:

答案 0 :(得分:0)

使用HashSet

class EmpListDemo
{
  public static void main(String arg[])
  {
      Set list = new HashSet();
      list.add(new Emp("Ram","Trainer",34000));
      list.add(new Emp("Sachin","Programmer",24000));
      list.add(new Emp("Ram","Trainer",34000));
      list.add(new Emp("Priyanka","Manager",54000));
      list.add(new Emp("Ravi","Administrator",44000));
      list.add(new Emp("Anupam","Programmer",34000));
      list.add(new Emp("Priyanka","Manager",54000));
      list.add(new Emp("Sachin","Team Leader",54000));
      System.out.println("There are "+list.size()+" elements in the list.");



      System.out.println("Content of list are : ");
      ListIterator itr1=list.listIterator();
      while(itr1.hasNext())
      {
        Emp e=(Emp)itr1.next();  
        e.display();
      }           

      }
}

答案 1 :(得分:0)

使用Set兄弟的任何实施方案.. 而不是允许重复,然后删除它; 防止你的数据结构甚至允许它们......