Java Utililty Methods Collection Null

List of utility methods to do Collection Null

Description

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

Method

intcountNonNull(Collection dist)
count Non Null
int count = 0;
for (Double val : dist) {
    if (val != null) {
        count++;
return count;
intcountNotNull(Collection collection)
count Not Null
int i = 0;
for (String item : collection) {
    if (item != null && !"!".equals(toKey(item))) {
        i++;
return i;
TgetItemAtPositionOrNull(Collection collection, int position)
Returns the n-th item or null if collection is smaller.
if (position >= collection.size()) {
    return null;
if (collection instanceof List) {
    return ((List<T>) collection).get(position);
Iterator<T> iterator = collection.iterator();
T item = null;
...
intgetNextNullIndex(C collection)
get Next Null Index
int output = -1;
int currentLoc = 0;
for (Object object : collection) {
    if (object == null) {
        output = currentLoc;
        break;
    currentLoc++;
...
intgetNumberOfNonNullElements(final Collection col)
get Number Of Non Null Elements
if (col == null)
    return 0;
int result = 0;
for (final Object o : col) {
    if (o != null) {
        result++;
return result;
EgetSingleElementOrNull(Collection collection)
returns the one and only element of a collection or null if the collection is empty.
if (collection.isEmpty()) {
    return null;
} else if (collection.size() > 1) {
    throw new IllegalStateException("collection has more than one elements");
} else {
    return collection.iterator().next();
intgetSizeNullSafe(final Collection collection)
Return the collection's size, or 0 if it is null
return (collection == null) ? 0 : collection.size();
booleanhasAtLeastOneNotNullElement(Collection collection)
has At Least One Not Null Element
if (collection == null)
    return false;
if (collection.isEmpty())
    return false;
if (collection.contains(null))
    return collection.size() > 1;
return collection.size() > 0;
booleanhasCollectionNullItem(Collection collection)
has Collection Null Item
boolean hasNull = false;
Iterator iterator = collection.iterator();
while (iterator.hasNext() && !hasNull) {
    Object object = iterator.next();
    if (object == null) {
        hasNull = true;
return hasNull;
booleanisAllNull(Collection values)
is All Null
boolean status = true;
for (String value : values) {
    if (value != null) {
        status = false;
        break;
return status;
...