Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy: what is analogue for java stream anyMatch

What is Groovy analogue for following operation?

list.stream().anyMatch(b -> b == 0); 
like image 661
Rudziankoŭ Avatar asked Oct 30 '17 16:10

Rudziankoŭ


2 Answers

Groovy syntax has a spectrum that ranges from Java-esque to idiomatic Groovy. Both of these work:

// Java-esque
List<Integer> list = [4,3,2,1,0]
assert list.stream().any{ b -> b == 0 }

// Groovier (note `it` is an alias for the parameter)
def list2 = [4,3,2,1,0]
assert list2.stream().any{ it == 0 }
like image 38
Michael Easter Avatar answered Sep 22 '22 03:09

Michael Easter


You mean to find if the list contains element 0?

def list = [0,1,2,3,4]
def result = list.any{it == 0}
println result

You can quickly try it online demo

like image 121
Rao Avatar answered Sep 19 '22 03:09

Rao