Many advice to use CollectionUtils.isNotEmpty(coll)
rather then coll != null
in the below use case also.
if (CollectionUtils.isNotEmpty(coll)) {
for (String str : coll) {
}
}
instead of
if (coll != null) {
for (String str : coll) {
}
}
Is there any reason/advantage to use CollectionUtils.isNotEmpty(coll)
instead of other here? Thanks.
isNotEmpty() method of CollectionUtils can be used to check if a list is not empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list.
Simply put, the Apache CollectionUtils provides utility methods for common operations which cover a wide range of use cases and helps in avoiding writing boilerplate code. The library targets older JVM releases because currently, similar functionality is provided by the Java 8's Stream API.
The standard solution to check if a Java Collection is empty is calling the isEmpty() method on the corresponding collection. It returns true if the collection contains no elements. The following solution provides the custom implementation of isEmpty() and isNotEmpty() methods, that handles null input gracefully.
No real advantages here. Even if there is, it would be extremely small. It just prevents the creation of an Iterator
and executing a branch instruction, that's all there is to it.
This small advantage occurs only when the collection is empty. The following loop:
for (String str : coll) {
...
}
is equivalent to:
for (Iterator<String> iterator = col.iterator(); iterator.hasNext();) {
String str = iterator.next();
...
}
When the collection is empty, the check on CollectionUtils.isNotEmpty(coll)
prevents the loop from executing. Hence no Iterator
is created in memory and no call to hasNext()
is done. This is at the expense to a O(1)
call to coll.isEmpty()
.
Decompiling reveals
public static boolean isEmpty(Collection coll) {
return coll == null || coll.isEmpty();
}
As explained above it depends, what you want to test and how is your logic constructed.
Suppose your example
if (CollectionUtils.isNotEmpty(coll)) {
for (String str : coll) {
System.out.println("Branch 1. Collection is not empty.");
}
}
else {
System.out.println("Branch 2. Collection is empty.");
}
In this example, we can see, that always Branch1 or Branch2 is executed.
If we use null expression, the result will be different if coll
is not null but empty
if (coll != null) {
for (String str : coll) {
System.out.println("Branch1. Collection is not empty.");
}
}
else {
System.out.println("Branch2. Collection is empty.");
}
If the collection coll
is not null but it is empty, nor Branch1 either Branch2 is executed, because the condition coll != null
is true, but in loop for
there is not even one pass.
Of course, if
expression coll != null && coll.isNotEmpty()
doing the same work as CollectionUtils.isNotEmpty(coll)
.
Therefore it not advisable programming manner to use test on null in case of collections coll != null
only. This is a case of poorly treated extreme conditions, which may be a source of unwanted result.
The issue is, that the collection can still be empty, when it is not null. So, in your case it depends on your preferences what you choose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With