Groovier: How to filter list in Groovy
To filter a list in groovy we can use the .find()
method on list and pass the closure as filtering criteria
Sample code snippets below
.find()
- To find a single value matching the critetia. Use findAll()
for multiple values
def lst = ["apple", "banana", "mangos"]
lst.find { it == "apple"} // Get only apple
Output
Result: banana
findAll()
- to find multiple results and filter nulls
def lstWithNulls = ["John", "Sonia", null, "David", "Andrew", null]
lstWithNulls.findAll()
Output
Result: [John, Sonia, David, Andrew]