首页 > 其他 > 详细

Judge

时间:2015-02-12 13:45:59      阅读:296      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!