Java Utililty Methods Collection Search

List of utility methods to do Collection Search

Description

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

Method

booleanhasLength(final Collection collection)
has Length
return collection != null && !collection.isEmpty();
booleanhasNoValue(Collection collection)
has No Value
if (collection == null)
    return true;
if (collection.isEmpty())
    return true;
for (Object val : collection) {
    if (val == null)
        return true;
    if (val instanceof String && ((String) val).isEmpty())
...
booleanhasOneItem(final Collection col)
has One Item
return col != null && col.size() == 1;
booleanhasOtherThan(Collection a, Collection b)
Return true if a contains at least one element which is not contained in b
if (a.size() > b.size())
    return true;
return !b.containsAll(a);
booleanhasValidElement(Collection collection)
has Valid Element
for (Object object : collection) {
    if (object != null) {
        return true;
return false;
booleanhasValue(Collection collection)
Is this a non-null collection w/ at least 1 item in it?
return (collection != null) && collection.size() > 0;
intindexOf(Collection stringCollection, String str)
Returns index of string in a string collection
if (str != null && stringCollection != null) {
    int i = 0;
    for (String s : stringCollection) {
        if (s.equals(str)) {
            return i;
        i++;
return -1;
intindexOf(final Collection c, final Object elem)
Get the index position of an element in a collection
if (c instanceof List) {
    return ((List) c).indexOf(elem);
} else {
    int index = 0;
    for (Object element : c) {
        if (element == elem) {
            return index;
        } else {
...
intindexOfInstance(final Collection collection, final T instance)
Determine the index position of the first occurrence of the given object instance in the given collection.
if (collection != null && !collection.isEmpty()) {
    int index = 0;
    for (final T collectionElement : collection) {
        if (collectionElement == instance) {
            return index;
        index++;
return -1;
intindexOfObjectIdentity(Collection collection, Object object)
Returns the index position of the first occurring object within the Collection .
int retval = -1;
if (collection != null) {
    Iterator<E> iterator = collection.iterator();
    if (iterator != null) {
        int indexPosition = 0;
        while (retval < 0 && iterator.hasNext()) {
            E element = iterator.next();
            if (element == object) {
...