两种方法删除ArrayList里反复元素

方法一:

/** List order not maintained **/

  public static void removeDuplicate(ArrayList arlList)
  {
   HashSet h = new HashSet(arlList);
   arlList.clear();
   arlList.addAll(h);
  }

方法二:


/** 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);
}


posted @ 2019-05-26 08:34  ldxsuanfa  阅读(111)  评论(0编辑  收藏  举报