Here you can find the source of removeDuplicates(Collection
Parameter | Description |
---|---|
elements | a parameter |
public static <T> List<T> removeDuplicates(Collection<T> elements)
//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; } }