Skip to content

Instantly share code, notes, and snippets.

@mangei
Last active March 24, 2021 04:11
Show Gist options
  • Save mangei/11169675 to your computer and use it in GitHub Desktop.
Save mangei/11169675 to your computer and use it in GitHub Desktop.
groovy null list and empty list check
package io.geier
class IfTest {
public static void main(String[] args) {
List<String> emptyList = [];
List<String> nullList = null;
if (emptyList == null || emptyList.empty) {
println "AAA emptyList"
}
if (emptyList?.empty) {
println "BBB emptyList"
}
println "-----------------"
if (nullList == null || nullList.empty) {
println "AAA nullList"
}
if (nullList?.empty) {
println "BBB nullList"
}
}
}
//
// OUTPUT:
// AAA emptyList
// BBB emptyList
// -----------------
// AAA nullList
//
@dbe-it
Copy link

dbe-it commented Aug 8, 2017

Why not using groovy truth?
if(nullList){...}
if(emptyList){...}
?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment