Chinaunix首页 | 论坛 | 博客
  • 博客访问: 579830
  • 博文数量: 718
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 4960
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-17 13:24
文章分类

全部博文(718)

文章存档

2011年(1)

2008年(717)

我的朋友

分类:

2008-10-17 13:33:42


  Here are two methods that allow you to remove duplicates in an ArrayList. removeDuplicate does not maintain the order where as removeDuplicateWithOrder maintains the order with some performance overhead.
  1.The removeDuplicate Method:
  /** List order not maintained **/
  public static void removeDuplicate(ArrayList arlList)
  {
    HashSet h = new HashSet(arlList);
    arlList.clear();
    arlList.addAll(h);
  }
  
  2.The removeDuplicateWithOrder Method:
  /** List order maintained **/
  public static void removeDuplicateWithOrder(ArrayList arlList)
  {
    Set set = new HashSet();
    List newList = new ArrayList();
    for (Iterator iter = arlList.iterator(); iter.hasNext(); )
    {
     Object element = iter.next();
     if (set.add(element)) newList.add(element);
    }
    arlList.clear();
    arlList.addAll(newList);
  }
  
【责编:admin】

--------------------next---------------------

阅读(189) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~