If you really want your includes method on an ArrayList, just add it:
ArrayList.metaClass.includes = { i -> i in delegate }
Some syntax sugar
1 in [1,2,3]
You can also use matches with regular expression like this:
boolean bool = List.matches("(?i).*SOME STRING HERE.*")
You can use Membership operator:
def list = ['Grace','Rob','Emmy']
assert ('Emmy' in list)
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.
For lists, use contains
:
[1,2,3].contains(1) == true
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.
Source: Stackoverflow.com