Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is CollectionUtils.isNotEmpty() better than a null check?

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.

like image 631
Trying Avatar asked Jun 05 '15 12:06

Trying


People also ask

Does CollectionUtils isNotEmpty check for null?

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.

What is the use of CollectionUtils in Java?

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.

How do I check if a collection is empty?

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.


4 Answers

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().

like image 168
M A Avatar answered Sep 29 '22 22:09

M A


Decompiling reveals

public static boolean isEmpty(Collection coll) {
    return coll == null || coll.isEmpty();
}
like image 27
taanielo Avatar answered Sep 28 '22 22:09

taanielo


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.

like image 29
hariprasad Avatar answered Sep 26 '22 22:09

hariprasad


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.

like image 23
NaN Avatar answered Sep 29 '22 22:09

NaN