利用了Set不重复的特性

方法一:

public static List 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);

}

return newList;

}

方法二:

public static List removeDuplicate(List list)

{

Set set = new LinkedHashSet();

set.addAll(list);

list.clear();

list.addAll(set);

return list;

}

方法三:

import java.util.*;

class sigleE

{

public static void main(String[] args)

{

ArrayList al = new ArrayList();

al.add("java1");

al.add("java2");

al.add("java1");

al.add("java3");

System.out.println(al);

al = list(al);

System.out.println(al);

}

public static ArrayList list(ArrayList al)

{

ArrayList newal = new ArrayList();//新建一个中间集合

for(Iterator it = al.iterator();it.hasNext();)//集合循环

{

Object obj = it.next();

if(!newal.contains(obj))//不包含重复的输出

newal.add(obj);

}

return newal;

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐