android List去掉重复数据

利用了Set不重复的特性

方法一:

[java]  view plain  copy
 print ?
  1. public static List removeDuplicateWithOrder(List list) {  
  2.         Set set = new HashSet();  
  3.         List newList = new ArrayList();  
  4.         for (Iterator iter = list.iterator(); iter.hasNext();) {  
  5.             Object element = iter.next();  
  6.             if (set.add(element))  
  7.                 newList.add(element);  
  8.         }  
  9.         return newList;  
  10.     }   

方法二:

[java]  view plain  copy
 print ?
  1. public static List<String> removeDuplicate(List<String> list)  
  2. {  
  3.        Set set = new LinkedHashSet<String>();  
  4.         set.addAll(list);  
  5.         list.clear();  
  6.         list.addAll(set);  
  7.         return list;  
  8. }   

方法三:

[java]  view plain  copy
 print ?
  1. import java.util.*;   
  2. class sigleE   
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         ArrayList al = new ArrayList();  
  7.         al.add("java1");  
  8.         al.add("java2");  
  9.         al.add("java1");  
  10.         al.add("java3");  
  11.         System.out.println(al);  
  12.         al = list(al);  
  13.         System.out.println(al);  
  14.     }  
  15.     public static ArrayList list(ArrayList al)  
  16.     {  
  17.         ArrayList newal = new ArrayList();//新建一个中间集合  
  18.         for(Iterator it = al.iterator();it.hasNext();)//集合循环  
  19.         {  
  20.             Object obj = it.next();  
  21.             if(!newal.contains(obj))//不包含重复的输出  
  22.                 newal.add(obj);  
  23.         }  
  24.         return newal;  
  25.     }  
  26. }  



转载:http://blog.csdn.net/chaoyu168/article/details/49273439

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值