/** * Returns all the non-{@code null} elements in the given {@link Collection}. * * @param <T> the type of elements of the {@code Collection}. * @param c the given {@code Collection}. * @return all the non-{@code null} elements in the given {@code Collection}. An empty list is returned if the * given {@code Collection} is {@code null}. * @since 1.1.3 */ public static <T> List<T> nonNullElementsIn(Collection<T> c) { if (isNullOrEmpty(c)) { return emptyList(); } List<T> nonNull = new ArrayList<T>(); for (T element : c) { if (element != null) { nonNull.add(element); } } return nonNull; }
/** * Returns any duplicate elements from the given {@code Collection}. * * @param <T> the generic type of the given {@code Collection}. * @param c the given {@code Collection} that might have duplicate elements. * @return a {@code Collection} containing the duplicate elements of the given one. If no duplicates are found, an * empty {@code Collection} is returned. */ public static <T> Collection<T> duplicatesFrom(Collection<T> c) { Set<T> duplicates = new LinkedHashSet<T>(); if (isNullOrEmpty(c)) { return duplicates; } Set<T> noDuplicates = new HashSet<T>(); for (T e : c) { if (noDuplicates.contains(e)) { duplicates.add(e); continue; } noDuplicates.add(e); } return duplicates; }