Summary of methods for removing duplicate data from List set in Java
- 2021-08-28 20:20:15
- OfStack
Overview of List Collection
The List collection is a set of ordered elements (each element has a corresponding order index, and the first element has an index of 0) and is repeatable.
Common Methods of List Set
List is a sub-interface of Collection interface, and has all the methods of Collection, as well as some methods of index operation.
void add (int index, E element); Insert element element into index of List collection; boolean addAll(int index, Collection < ? extends E > c); Insert all elements of the collection c into the beginning of index of the collection List; E remove (int index); Removes and returns the element at index; int indexOf (Object o); Returns the index of the first occurrence of the object o in the List collection; int lastIndexOf (Object o); Returns the index of the last occurrence of the object o in the List collection; E set (int index, E element); Replaces the element at the index index with the new element object and returns the replaced old element; E get (int index); Returns the object at the index index of the collection; List < E > subList (int fromIndex, int toIndex); Returns a subset of all elements from index fromIndex (included) to index toIndex (not included); void sort(Comparator < ? super E > c): Sorting List set elements according to Comparator parameters; void replaceAll(UnaryOperator < E > operator): Resets all elements of the collection according to the evaluation rules specified in operator. ListIterator < E > listIterator (); Returns an ListIterator object that inherits the Iterator interface, adding the following methods to the Iterator interface, iterating forward and adding elements: bookean hasPrevious (): Returns whether the set associated with the iterator has the last 1 element; E previous (); Returns 1 element on the iterator; void add (E e); Inserts an element at a specified location;Java List Weight Removal
1. Loop all elements in list and remove duplicates
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;
}
2. Kick out duplicate elements through HashSet
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}
3. Remove duplicate elements from ArrayList, preserving order
// Delete ArrayList Repeat elements in, keeping order
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. Traverse the object in list once, use list. contain (), and put it into another list collection if it doesn't exist
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.add(list.get(i));
}
}
return listTemp;
}