138

I need to perform a null or empty check on a collection; I think that !members?.empty is incorrect. Is there a groovier way to write the following?

if (members && !members.empty) {
    // Some Work
}
alex
  • 6,818
  • 9
  • 52
  • 103
Jay Bose
  • 1,511
  • 2
  • 11
  • 14

3 Answers3

282

There is indeed a Groovier Way.

if (members) {
    //Some work
}

does everything if members is a collection. Null check as well as empty check (Empty collections are coerced to false). Hail Groovy Truth. :)

Elijah Lynn
  • 12,272
  • 10
  • 61
  • 91
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • 5
    A more "groovier" way is that for example if you are interested in the maximum age out of the members then you can write the following: members?.age.max() – BTakacs Aug 19 '14 at 11:54
  • 13
    Note: `members?.age.max()` blows up with "Cannot invoke method max() on null object" when members is null. You would need `members?.age?.max()` – GreenGiant Aug 22 '14 at 15:10
  • @BTakacs I think you mean `members*.age.max()` – Muhd Jan 05 '16 at 21:01
  • 2
    no: GreenGiant's solution is the best: check `List members = null;` and `List members = [ [age: 12], [age: 24], [age: null], null ]` against both the solutions – BTakacs Jan 07 '16 at 15:20
  • 2
    This type of check is working for most of the cases but if your purpose is to check if a variable is null then you might end up to an edge case where the variable is not null but a boolean false – NikosDim May 25 '17 at 11:59
  • this solution is perfect for the case where multiple statements needs this check. it works fine for list also for checking not-null and non-empty. for single statement I prefer to use null-safe operator as GreenGiant suggests – imdzeeshan Dec 11 '17 at 14:50
  • Can we test if a parameter is `null` in this way? `if(!members){ // do something }` – robothy Apr 09 '21 at 03:25
  • @robothy You will run into issues if you try `!members` if your intention is to do something only when members is `null`. See the examples [here which showcases how Groovy Truth works](http://groovy-lang.org/semantics.html#Groovy-Truth). It will be better if you explicitly check for null `members == null` – dmahapatro Apr 09 '21 at 13:39
2

FYI this kind of code works (you can find it ugly, it is your right :) ) :

def list = null
list.each { println it }
soSomething()

In other words, this code has null/empty checks both useless:

if (members && !members.empty) {
    members.each { doAnotherThing it }
}

def doAnotherThing(def member) {
  // Some work
}
Max
  • 511
  • 8
  • 18
  • This code will fail on line 2 null.each will throw an NPE. The second block of code uses the original user's post. I think you may have misunderstood the question. – Chewy Mar 02 '23 at 02:28
0
!members.find()

I think now the best way to solve this issue is code above. It works since Groovy 1.8.1 http://docs.groovy-lang.org/docs/next/html/groovy-jdk/java/util/Collection.html#find(). Examples:

def lst1 = []
assert !lst1.find()

def lst2 = [null]
assert !lst2.find()

def lst3 = [null,2,null]
assert lst3.find()

def lst4 = [null,null,null]
assert !lst4.find()

def lst5 = [null, 0, 0.0, false, '', [], 42, 43]
assert lst5.find() == 42

def lst6 = null; 
assert !lst6.find()
Zhurov Konstantin
  • 712
  • 1
  • 8
  • 14