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

voidaddUnique(C collection, T item)
add Unique
if (!collection.add(item)) {
    throw new IllegalStateException("Encountered an already added item:" + item
            + " when adding items uniquely to a collection:" + collection);
booleanaddUnique(final Collection collection, final Object value)
add a unique value to a collection.
if (collection.contains(value)) {
    return false;
return collection.add(value);
StringgenerateUniqueName(String aName, Collection aStringCollection)
Generates a unique name for the child.
return generateUniqueName(aName, aStringCollection, false);
TgetUnique(Collection c)
get Unique
if (c.isEmpty()) {
    return null;
} else {
    return c.iterator().next();
TgetUnique(Collection collection)
Returns the only element in the given collection.
checkNull("Collection", collection);
if (collection.size() != 1) {
    throw new IllegalArgumentException(
            "Collection " + collection + " is expected to contain only one element");
return collection.iterator().next();
StringgetUniqueName(String name, Collection collection)
this gets a new, unique name given a string and an existing collection The name will be incremented by adding "x", where x is an integer, until a unique name is found
if (collection == null) {
    collection = Collections.EMPTY_SET;
String result = name;
int incr = 1;
boolean nameIsInCollection = false;
do {
    nameIsInCollection = false;
...
StringgetUniqueNameWithNumbers(Collection names, String baseName)
get Unique Name With Numbers
if (names == null) {
    return baseName;
String name = baseName;
int i = 1;
while (names.contains(name)) {
    name = baseName + i;
    i++;
...
StringgetUniqueValue(Collection values, String initValue)
Makes the value passed in initValue unique among the String values contained in values by suffixing it with a decimal digit suffix.
if (!values.contains(initValue)) {
    return initValue;
} else {
    StringBuffer unqVal = new StringBuffer(initValue);
    int beg = unqVal.length(), cur, end;
    while (Character.isDigit(unqVal.charAt(beg - 1))) {
        beg--;
    if (beg == unqVal.length()) {
        unqVal.append('1');
    cur = end = unqVal.length() - 1;
    while (values.contains(unqVal.toString())) {
        if (unqVal.charAt(cur) < '9') {
            unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1));
        } else {
            while (cur-- > beg) {
                if (unqVal.charAt(cur) < '9') {
                    unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1));
                    break;
            if (cur < beg) {
                unqVal.insert(++cur, '1');
                end++;
            while (cur < end) {
                unqVal.setCharAt(++cur, '0');
    return unqVal.toString();
booleanhasUniqueObject(Collection collection)
Determine whether the given Collection only contains a single unique object.
if (isEmpty(collection)) {
    return false;
boolean hasCandidate = false;
Object candidate = null;
for (Object elem : collection) {
    if (!hasCandidate) {
        hasCandidate = true;
...
booleanhasUniqueObject(Collection collection)
has Unique Object
if (isEmpty(collection)) {
    return false;
boolean hasCandidate = false;
Object candidate = null;
for (Object elem : collection) {
    if (!hasCandidate) {
        hasCandidate = true;
...