Search code examples
grailsgroovy

Checking if a collection is null or empty in Groovy


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
}

Solution

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