1. 循环list中的所有元素然后删除重复
publicstaticListremoveDuplicate(Listlist){
for(inti=0;i
for(intj=list.size()-1;j>i;j--){
if(list.get(j).equals(list.get(i))){
list.remove(j);
}
}
}
returnlist;
}
2. 通过HashSet踢除重复元素
publicstaticListremoveDuplicate(Listlist){
HashSeth=newHashSet(list);
list.clear();
list.addAll(h);
returnlist;
}
3.删除ArrayList中重复元素,保持顺序
//删除ArrayList中重复元素,保持顺序
publicstaticvoidremoveDuplicateWithOrder(Listlist){
Setset=newHashSet();
ListnewList=newArrayList();
for(Iteratoriter=list.iterator();iter.hasNext();){
Objectelement=iter.next();
if(set.add(element))
newList.add(element);
}
list.clear();
list.addAll(newList);
System.out.println("removeduplicate"+list);
}
4.把list里的对象遍历一遍,用list.contain(),如果不存在就放入到另外一个list集合中
publicstaticListremoveDuplicate(Listlist){
ListlistTemp=newArrayList();
for(inti=0;i
if(!listTemp.contains(list.get(i))){
listTemp.add(list.get(i));
}
}
returnlistTemp;
}