您的位置:首页 > 其它

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

2009-03-20 22:49 525 查看
1.方法一: Java代码 /** List order not maintained **/ public static void removeDuplicate(ArrayList arlList) { HashSet h = new HashSet(arlList); arlList.clear(); arlList.addAll(h); }
2.方法二: Java代码 /** 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);   }  

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: