标签:重复数 ref 重复元素 move dal als nta https hash
https://blog.csdn.net/u011728105/article/details/46594963
public static List removeDuplicate(List list) {
for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {
for ( int j = list.size() - 1 ; j > i; j -- ) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
return list;
}
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return 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);
}
4.把list里的对象遍历一遍,用list.contain(),如果不存在就放入到另外一个list集合中
public static List removeDuplicate(List list){
List listTemp = new ArrayList();
for(int i=0;i<list.size();i++){
if(!listTemp.contains(list.get(i))){ //listTemp中保存的就是不重复的数据
listTemp.add(list.get(i));
}
else{
//也可以把重复的数据提取出来。
}
}
return listTemp;
标签:重复数 ref 重复元素 move dal als nta https hash
原文地址:https://www.cnblogs.com/sunny3158/p/14327683.html