/** * Returns any duplicate elements from the given collection. * * @param <T> the generic type of the given collection. * @param c the given collection that might have duplicate elements. * @return a collection containing the duplicate elements of the given one. If no duplicates are * found, an empty collection is returned. */ public static <T> Collection<T> duplicatesFrom(Collection<T> c) { Set<T> duplicates = new HashSet<T>(); if (isEmpty(c)) return duplicates; Set<T> onlyOne = new HashSet<T>(); for (T e : c) { if (onlyOne.contains(e)) { duplicates.add(e); continue; } onlyOne.add(e); } return duplicates; }
/** * Creates a set containing the given elements. * * @param <T> the type of elements of the set to create. * @param elements the elements to store in the set. * @return the created set. * @since 1.1.5 */ public static <T> Set<T> set(T... elements) { if (elements == null) return null; Set<T> set = new LinkedHashSet<T>(); for (T e : elements) set.add(e); return set; }