8

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.

Trying
  • 14,004
  • 9
  • 70
  • 110
  • 2
    In this specific case, no. But sometimes you do other operations before the loop, like preparing structures. – Denys Séguret Jun 05 '15 at 12:27
  • The related question about null checks: http://stackoverflow.com/questions/271526/ – blgt Jun 05 '15 at 12:29
  • @DenysSéguret, in this specific case it *does* make a difference. The `for (String str : coll)` will throw a `NullPointerException` if `coll` is `null`. – aioobe Jun 05 '15 at 12:31
  • @aioobe There's a test for that. The question is about the difference between the two tests – Denys Séguret Jun 05 '15 at 12:33
  • @aioobe this is not a duplicate to "check if a collection is empty in java : Which is the best method " – Trying Jun 05 '15 at 12:34
  • @DenysSéguret, aah, sorry. You're right. – aioobe Jun 05 '15 at 12:34
  • @Trying, question reopened, but I still think the answers over [here](http://stackoverflow.com/questions/11152536/check-if-a-collection-is-empty-in-java-which-is-the-best-method) pretty much covers this question. – aioobe Jun 05 '15 at 12:37
  • @aioobe this is little different. I am asking: is first call to `CollectionUtils.isNotEmpty` then go for `for` loop better than if we simply check for null and go for `for` loop ... As @manouti stated in the answer there is some. Even tough it is very very small... – Trying Jun 05 '15 at 12:40
  • -ve voter please comment at least whats wrong. – Trying Jun 05 '15 at 12:51

4 Answers4

9

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

M A
  • 71,713
  • 13
  • 134
  • 174
7

Decompiling reveals

public static boolean isEmpty(Collection coll) {
    return coll == null || coll.isEmpty();
}
taanielo
  • 189
  • 6
2

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.

hariprasad
  • 555
  • 11
  • 20
1

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.

NaN
  • 7,441
  • 6
  • 32
  • 51