Created
February 24, 2012 13:22
-
-
Save timyates/1900906 to your computer and use it in GitHub Desktop.
What's new in Groovy 1.8.6: Array.contains()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Uses the pure Java method | |
| assert list.contains( 2 ) | |
| // Uses a method added by Groovy 1.8.6 | |
| assert array.contains( 2 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if( !( element in array ) ) { | |
| // do something | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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