Wednesday, 23 January 2019

Groovy: List: every(): Return true if all the elements in the list matches to given criteria


Groovy provides every method, it returns true, if all the elements in the list matches to given criteria, else false.

public boolean every(Closure predicate)
Determine if the given predicate closure is valid for all the elements of the list.

HelloWorld.groovy

evenNumbers = [2, 4, 6, 8]
numbers = [0, 1, 2, 3, 4, 5]

isAllevenNumbers1 = evenNumbers.every { it % 2 == 0}
isAllevenNumbers2 = numbers.every { it % 2 == 0}

println "Is evenNumbers contain all the even numbers : $isAllevenNumbers1"
println "Is numbers contain all the even numbers : $isAllevenNumbers2"

Output
Is evenNumbers contain all the even numbers : true
Is numbers contain all the even numbers : false


Previous                                                 Next                                                 Home

No comments:

Post a Comment