个人笔记-----list去重顺序不变

// 删除ArrayList中重复元素,保持顺序
    public static void removeDuplicateWithOrder(List list){
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        System.out.println( " remove duplicate " + list);
    }

 

你可能感兴趣的:(笔记)