Wednesday, 23 January 2019

Groovy: List: any: Check whether any one element matches to given criteria


Groovy provides ‘any’ method, it returns true if any element in the collection matches to given criteria, else false.

public boolean any(Closure predicate)
Return true, if any element matches to given criteria, else false.

HelloWorld.groovy

numbers = [0, 1, 2, 3, 4, 5]
oddNumbers = [1, 3, 5, 7]

isAnyEvenNumber1 = numbers.any { it % 2 == 0}
isAnyEvenNumber2 = oddNumbers.any { it % 2 == 0}

println "Is numbers contain any even numbers : $isAnyEvenNumber1"
println "Is oddNumbers contain any even numbers : $isAnyEvenNumber2"

Output
Is numbers contain any even numbers : true
Is oddNumbers contain any even numbers : false


Previous                                                 Next                                                 Home

No comments:

Post a Comment