Skip to content

Instantly share code, notes, and snippets.

@timyates
Created February 24, 2012 13:22
Show Gist options
  • Select an option

  • Save timyates/1900906 to your computer and use it in GitHub Desktop.

Select an option

Save timyates/1900906 to your computer and use it in GitHub Desktop.
What's new in Groovy 1.8.6: Array.contains()
// Given a List of BigDecimals
List bewareList = [ 1.1, 2.0, 3.1 ]
// And a matching array of double
double[] bewareArray = bewareList as double[]
def ival = 2 // This will be a java.lang.Integer
def dval = 2.0 // And this, a java.math.BigDecimal
// Both of these pass
assert bewareArray.contains( dval )
assert bewareList.contains( dval )
// This passes
assert bewareArray.contains( ival )
// But this fails, as the Integer object 2 isn't contained
assert bewareList.contains( ival )
// Uses the pure Java method
assert list.contains( 2 )
// Uses a method added by Groovy 1.8.6
assert array.contains( 2 )
if( !( element in array ) ) {
// do something
}
List list = [ 1, 2, 3 ]
int[] array = [ 1, 2, 3 ]
assert list.class.name == 'java.util.ArrayList'
assert array.class.name == '[I'
assert list.collect { it * 2 } == [ 2, 4, 6 ]
assert array.collect { it * 2 } == [ 2, 4, 6 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment