Java Utililty Methods Collection Unique

List of utility methods to do Collection Unique

Description

The list of methods to do Collection Unique are organized into topic(s).

Method

booleanhasUniqueObject(Collection collection)
CollectionUtils Determine whether the given Collection only contains a single unique object.
if (collection == null || collection.isEmpty()) {
    return false;
boolean hasCandidate = false;
Object candidate = null;
for (Object elem : collection) {
    if (!hasCandidate) {
        hasCandidate = true;
...
Collectionunique(Collection c)
unique
if (c != null && c.size() > 0) {
    Map<T, Integer> map = new LinkedHashMap<T, Integer>();
    for (T o : c) {
        map.put(o, 0);
    return new ArrayList<T>(map.keySet());
return c;
...
Collectionunique(Collection c, Collection result)
unique
if (c == null) {
    return result;
Set<T> s = new LinkedHashSet<T>(c);
result.addAll(s);
return result;
StringuniqueIdNotIn(String prefix, Collection exclusions)
unique Id Not In
StringBuilder sb = new StringBuilder(prefix);
for (int counter = 0;; ++counter) {
    sb.setLength(prefix.length());
    sb.append('.').append(counter);
    String candidate = sb.toString();
    if (!exclusions.contains(candidate)) {
        return candidate;
TuniqueResult(Collection results)
Method to take a get a unique result from a set and return it or null.
return results.isEmpty() ? null : results.iterator().next();
TuniqueResult(final Collection c)
unique Result
switch (c.size()) {
case 0:
    return null;
case 1:
    return c.iterator().next();
default:
    throw new IllegalStateException("Unexpected number of elements in collection: " + c.size());