Java Collection Remove removeDuplicates(Collection elements)

Here you can find the source of removeDuplicates(Collection elements)

Description

This does not change the passed in elements collection, it returns a new list Compared to just using Set, this preserves the order of the passed in collection

License

Open Source License

Parameter

Parameter Description
elements a parameter

Declaration

public static <T> List<T> removeDuplicates(Collection<T> elements) 

Method Source Code

//package com.java2s;

import java.util.*;
import java.util.List;

public class Main {
    /**//from   ww w  . ja va 2  s  .c  om
     * This does not change the passed in elements collection, it returns a new
     * list Compared to just using Set, this preserves the order of the passed
     * in collection
     *
     * @param elements
     * @return
     */
    public static <T> List<T> removeDuplicates(Collection<T> elements) {
        Set<T> added = new HashSet<>();
        List<T> res = new ArrayList<>();
        for (T e : elements) {
            if (!added.contains(e)) {
                res.add(e);
                added.add(e);
            }
        }
        return res;
    }
}

Related

  1. removed(Collection a, Collection b)
  2. removeDuplicates(Collection collection)
  3. removeDuplicates(Collection original)
  4. removeDuplicates(Collection p_collection)
  5. removeDuplicates(Collection objects)
  6. removeElement(Collection coll)
  7. removeElement(final int index, final Collection coll)
  8. removeElementsOfList(Collection set, Collection elementsToRemove)
  9. removeIfNotPresent(Collection collection, Collection permitted)