Quick Null Checks in Java with StringUtils, ObjectUtils, CollectionUtils

This article explains how to replace repetitive !=null checks in Java by identifying the data type and using the appropriate utility classes—StringUtils for strings, ObjectUtils for objects, and CollectionUtils for collections—to perform concise and reliable null or empty evaluations, including code examples and discussion of edge cases.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Quick Null Checks in Java with StringUtils, ObjectUtils, CollectionUtils

NullPointerException is a common bug; developers often add !=null checks, but there are more efficient ways.

Step 1: Identify the data type (String, Object, List, Array, Map, etc.).

Step 2: Choose the appropriate utility class: StringUtils for strings, ObjectUtils for objects, Collections/CollectionUtils for collections.

Step 3: Use the utility method to perform the null/empty check.

String str = "";
StringUtils.isEmpty(str); // true
Object obj = null;
ObjectUtils.isEmpty(obj); // true
Map<String,Object> map = Collections.emptyMap();
ObjectUtils.isEmpty(map); // true
List<Integer> list = Collections.EMPTY_LIST;
ObjectUtils.isEmpty(list); // true
Object[] objArr = null;
ObjectUtils.isEmpty(objArr); // true

The core method in ObjectUtils.isEmpty checks for null, Optional, CharSequence, arrays, collections, and maps, returning true when the object is considered empty.

However, this method only checks collection size, not whether elements themselves are null. For a List containing a single null element, ObjectUtils.isEmpty returns false.

List<Integer> list = Collections.singletonList(null);
ObjectUtils.isEmpty(list); // false

To verify that every element is null, iterate over the collection or convert it to an array and check each element, e.g., using Arrays.stream.

Arrays.stream(list.toArray()).allMatch(ObjectUtils::isEmpty);

For maps, CollectionUtils.isEmpty combines a null check and an isEmpty check.

public static boolean isEmpty(@Nullable Map<?,?> map) {
    return map == null || map.isEmpty();
}

Similarly, CollectionUtils.isEmpty for collections checks for null and emptiness.

public static boolean isEmpty(@Nullable Collection<?> collection) {
    return collection == null || collection.isEmpty();
}

In summary, determine the data type, select the corresponding utility class (StringUtils for strings, ObjectUtils for other types, CollectionUtils for collections), and use its isEmpty method; for collections or arrays where each element must be non‑null, iterate and check each element individually.

JavaUtilityStringUtilsnull checkObjectUtilsCollectionUtils
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.