Java List Remove Duplicate removeDuplicates(List list)

Here you can find the source of removeDuplicates(List list)

Description

remove Duplicates

License

Open Source License

Declaration

public static void removeDuplicates(List list) 

Method Source Code


//package com.java2s;

import java.util.Collection;
import java.util.Collections;

import java.util.HashSet;

import java.util.Iterator;
import java.util.List;

import java.util.Set;

public class Main {

    public static void removeDuplicates(List list) {
        if ((list != null) && !list.isEmpty()) {
            Set set = new HashSet(list.size());

            for (Iterator iter = list.iterator(); iter.hasNext();) {
                Object element = iter.next();

                if (set.contains(element)) {
                    iter.remove();//from  w w  w  . j a v  a 2 s  .  co  m
                } else {
                    set.add(element);
                }
            }
        }
    }

    /**
     * Returns 0 if the specified collection is null or the collection size
     * if not null.
     *
     * @param c the collection to size
     * @return the size of the collection
     */
    public static int size(Collection c) {
        return (c == null) ? 0 : c.size();
    }

    /**
     * Returns an iterator on a collection even null.
     * @param collection Collection
     * @return Iterator
     */
    public static Iterator iterator(Collection collection) {
        if (collection == null) {
            return Collections.EMPTY_LIST.iterator();
        } else {
            return collection.iterator();
        }
    }
}

Related

  1. removeDup(List lst)
  2. removeDuplicate(Collection entityList)
  3. removeDuplicate(List uidList)
  4. removeDuplicate(List dest, List src)
  5. removeDuplicates(int[] list)
  6. removeDuplicates(List listofthings)
  7. removeDuplicates(List commonVars)
  8. removeDuplicates(List l)
  9. removeDuplicates(List collection)