Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if List has empty strings in Groovy

Tags:

groovy

In Groovy, I am taking the values from a Map and creating a List from this. I then want to check if that List (of values) is empty.

My whole aim is to check if ALL the values of myMap are blank or empty.

In the code below, the values of myMap will either be an empty string or have some value.

Map myMap = [:]
myMap["a"] = "$A"
myMap["b"] = "$B"
myMap["c"] = "$C"

List myList = myMap.values() //[, , , ]

myList returns [, , , ]. How do I check this List contains all empty strings or do I go about this a whole other way?

Thanks

like image 546
user9393908 Avatar asked Dec 23 '22 09:12

user9393908


1 Answers

Use every

myList.every { it == '' }

To check if any of them are '', use any

myList.any { it == '' }
like image 67
tim_yates Avatar answered Jan 04 '23 02:01

tim_yates