码迷,mamicode.com
首页 > 其他好文 > 详细

Judge

时间:2015-02-12 13:48:01      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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]  

 

Judge

标签:

原文地址:http://www.cnblogs.com/a757956132/p/4287812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!