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.
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:
<code>String str = "";
StringUtils.isEmpty(str); // true</code> <code>public static boolean isEmpty(@Nullable Object str) {
return str == null || "".equals(str);
}</code>Object example:
<code>Object obj = null;
ObjectUtils.isEmpty(obj); // true</code>Map example:
<code>Map<String,Object> map = Collections.emptyMap();
ObjectUtils.isEmpty(map); // true</code>List example:
<code>List<Integer> list = Collections.EMPTY_LIST;
ObjectUtils.isEmpty(list); // true</code>Array example:
<code>// array
Object[] objArr = null;
ObjectUtils.isEmpty(objArr); // true</code>Source of ObjectUtils.isEmpty:
<code>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;
}</code>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.
<code>List<Integer> list = Collections.singletonList(null);
ObjectUtils.isEmpty(list); // false</code>For such cases, iterate over elements or use Streams:
<code>Arrays.stream(list.toArray()).allMatch(ObjectUtils::isEmptyisNull);</code>Map null/empty check can be simplified with CollectionUtils:
<code>public static boolean isEmpty(@Nullable Map<?, ?> map) {
return map == null || map.isEmpty();
}</code>List null/empty check with CollectionUtils:
<code>public static boolean isEmpty(@Nullable Collection<?> collection) {
return collection == null || collection.isEmpty();
}</code>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.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.