Judge

1. 循环list中的所有元素然后删除重复 

[java] view plaincopy
 
  1. public   static   List  removeDuplicate(List list)  {     
  2.   for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {     
  3.       for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {     
  4.            if  (list.get(j).equals(list.get(i)))  {     
  5.               list.remove(j);     
  6.             }      
  7.         }      
  8.       }      
  9.     return list;     
  10. }    

2. 通过HashSet踢除重复元素

[c-sharp] view plaincopy
 
  1. public   static   List  removeDuplicate(List list)  {     
  2.     HashSet h  =   new  HashSet(list);     
  3.     list.clear();     
  4.     list.addAll(h);     
  5.     return list;     
  6. }     

在groovy中当然也可以使用上面的两种方法, 但groovy自己提供了unique方法来去除重复数据 

[c-sharp] view plaincopy
 
  1. def list = [1, 2, 3, 2, 4, 1, 5]     
  2. list.unique()  // [1, 2, 3, 4, 5]  
原文地址:https://www.cnblogs.com/a757956132/p/4287812.html