169

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.

2
  • Can you find the index out also of where this equal element is in the list? Commented Mar 30, 2012 at 11:53
  • 2
    @AtharvaJohri assert [12,42,33].indexOf(42) == 1 Commented Mar 30, 2012 at 19:09

8 Answers 8

297

Some syntactic sugar

1 in [1,2,3]
Sign up to request clarification or add additional context in comments.

6 Comments

Careful. def m = [a: true]; 'a' in m → true yet def m = [a: false]; 'a' in m → false!
How do you negate this?
@BigMcLargeHuge !(1 in [1,2,3])
The way to deal with [a:false] is to use contains. [a: false].containsKey('a') -> true
@Big McLargeHuge negation is also possible like this: 1 !in [1,2,3] (Membership operator Groovy)
|
163

.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')

1 Comment

And, in addition, to check if a map contains some not null value under a certain key, it is enough to check the following expression if(aMap["aKey"]==aValue).
74

For lists, use contains():

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

2 Comments

Probably you wanted to say [1,2,3].contains(1). Because I am guessing contains function itself already returns a boolean. Why do you want to again compare it with a hardcoded 'true'.
@HarshayBuradkar To make really sure true == true, of course #joke
13

You can use Membership operator:

def list = ['Grace','Rob','Emmy']
assert ('Emmy' in list)  

Membership operator Groovy

Comments

9

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

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

Comments

3

IMPORTANT Gotcha for using .contains() on a Collection of Objects, such as Domains. If the Domain declaration contains a EqualsAndHashCode, or some other equals() implementation to determine if those Ojbects are equal, and you've set it like this...

import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode(includes = "settingNameId, value")

then the .contains(myObjectToCompareTo) will evaluate the data in myObjectToCompareTo with the data for each Object instance in the Collection. So, if your equals method isn't up to snuff, as mine was not, you might see unexpected results.

Comments

3
def fruitBag = ["orange","banana","coconut"]
def fruit = fruitBag.collect{item -> item.contains('n')}

I did it like this so it works if someone is looking for it.

Comments

-1

You can also use matches with regular expression like this:

boolean bool = List.matches("(?i).*SOME STRING HERE.*")

1 Comment

Can you explain how this works? AFAIK there is no 'matches' method on list: 'groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.matches()'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.