list删除重复记录

两种比较实用的方法,一个是双循环,一个是hashset.

一.循环

public   static   List  removeDuplicate(List list)  {   
  for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {   
      for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {   
           if  (list.get(j).equals(list.get(i)))  {   
              list.remove(j);   
            }    
        }    
      }    
    return list;   
} 


二.hastset

public   static   List  removeDuplicate(List list)  {   
    HashSet h  =   new  HashSet(list);   
    list.clear();   
    list.addAll(h);   
    return list;   
 }  


你可能感兴趣的:(list)