-2

可能重复:
以不同方式从列表中删除重复项

我正在做一项研究,并在下面找到从列表中删除重复项的方法..但请告知是否还有其他方法,因为我正在对此进行研究,我也想在集合中探索其他方法,这将是一个太棒了..我对我的研究更感兴趣的是新的 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();
          }   




              }
    }
4

2 回答 2

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();
      }           

      }
}
于 2012-08-01T17:21:57.067 回答
0

使用SetBrother的任何实现..而不是允许重复然后删除它;防止您的数据结构甚至允许它们...

于 2012-08-01T17:24:17.560 回答