Fundamentals 8 min read

Master Elegant Null Checks in Java with Utility Classes

This article explains how to replace repetitive !=null checks in Java by identifying data types and using the appropriate Spring/Apache utility classes such as StringUtils, ObjectUtils, Collections, and CollectionUtils, providing code examples and best‑practice guidelines.

macrozheng
macrozheng
macrozheng
Master Elegant Null Checks in Java with Utility Classes

NullPointerException is common; many developers add !=null checks, but this article shows efficient, elegant ways using Java utility classes.

Step 1

Identify the data type (String, Object, List, Array, Map, etc.) before performing a null check.

Step 2

Choose the appropriate utility class for the type:

String → StringUtils

Object → ObjectUtils

Array → Arrays

List, Map → Collections, CollectionUtils, etc.

Step 3

Use the selected utility class to perform the check.

Examples:

String str = "";
StringUtils.isEmpty(str); // true
public static boolean isEmpty(@Nullable Object str) {
    return str == null || "".equals(str);
}

Object example:

Object obj = null;
ObjectUtils.isEmpty(obj); // true

Map example:

Map<String,Object> map = Collections.emptyMap();
ObjectUtils.isEmpty(map); // true

List example:

List<Integer> list = Collections.EMPTY_LIST;
ObjectUtils.isEmpty(list); // true

Array example:

// array
Object[] objArr = null;
ObjectUtils.isEmpty(objArr); // true

Source of ObjectUtils.isEmpty:

public static boolean isEmpty(@Nullable Object obj) {
    if (obj == null) {
        return true;
    }
    if (obj instanceof Optional) {
        return !((Optional) obj).isPresent();
    }
    if (obj instanceof CharSequence) {
        return ((CharSequence) obj).length() == 0;
    }
    if (obj.getClass().isArray()) {
        return Array.getLength(obj) == 0;
    }
    if (obj instanceof Collection) {
        return ((Collection) obj).isEmpty();
    }
    if (obj instanceof Map) {
        return ((Map) obj).isEmpty();
    }
    return false;
}

This method covers Optional, CharSequence, arrays, collections, and maps, but it only checks size, so a List with a single null element is considered non‑empty.

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

For such cases, iterate over elements or use Streams:

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

Map null/empty check can be simplified with CollectionUtils:

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

List null/empty check with CollectionUtils:

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

Final Summary

To determine whether a value is null or empty, follow three steps: identify its type, select the proper utility class, and invoke the appropriate method. Use StringUtils for strings; use ObjectUtils (or CollectionUtils) for other types; for collections or arrays, iterate when you need to check each element.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

coding best practicesStringUtilsnull checkObjectUtils
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.