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:
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); // trueMap example:
Map<String,Object> map = Collections.emptyMap();
ObjectUtils.isEmpty(map); // trueList example:
List<Integer> list = Collections.EMPTY_LIST;
ObjectUtils.isEmpty(list); // trueArray example:
// array
Object[] objArr = null;
ObjectUtils.isEmpty(objArr); // trueSource 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); // falseFor 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
