Checking if a collection is null or empty in Groovy
Asked Answered
A

3

141

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
}
Avi answered 23/6, 2013 at 1:15 Comment(0)
K
288

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

Kanal answered 23/6, 2013 at 1:18 Comment(8)
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()Nuristan
Note: members?.age.max() blows up with "Cannot invoke method max() on null object" when members is null. You would need members?.age?.max()Dickman
@Nuristan I think you mean members*.age.max()Kile
no: GreenGiant's solution is the best: check List members = null; and List members = [ [age: 12], [age: 24], [age: null], null ] against both the solutionsNuristan
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 falseGalbanum
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 suggestsWallah
Can we test if a parameter is null in this way? if(!members){ // do something }Tisatisane
@Tisatisane 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. It will be better if you explicitly check for null members == nullKanal
G
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
}
Garretgarreth answered 11/11, 2019 at 10:58 Comment(1)
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.Ils
E
1
!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()
Event answered 28/5, 2019 at 13:31 Comment(3)
the collection that has 1 null element is not empty, so your suggestion is wrongKronfeld
What if the collection is null?Dorado
def lst6 = null; assert !lst6.find() it's correct - no error occursEvent

© 2022 - 2024 — McMap. All rights reserved.