利用了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<String> removeDuplicate(List<String> list) { Set set = new LinkedHashSet<String>(); 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; } }