views:

2931

answers:

4

How do I figure out if an array contains an element? I thought there might be something like [1,2,3].includes(1) which would evaluate as 'true'

+3  A: 

[1,2,3].contains(1) == true

banderson623
+9  A: 

Some syntax sugar

1 in [1,2,3] == true

dahernan
+2  A: 

.contains() is the best method for lists, but for maps you will need to use .containsKey() or .containsValue()

[a:1,b:2,c:3].containsValue(3)
[a:1,b:2,c:3].containsKey('a')
shemnon
+1  A: 

If you really want your includes method on an ArrayList, just add it:

ArrayList.metaClass.includes = { i -> i in delegate }
John Flinchbaugh