|
versions of Groovy 2:
main
| yours | all
More lists
| Question | Answer |
| merge 2 lists | l + myOtherList |
| subtract lists | l - myOtherList |
| loop 3 times | 3.times() { println it } |
| grep a list | l.grep( /a/ ) |
| find largest value (in a list) | l.max() |
| find values above 1 (in a list) | [1, 2, 3].findAll { it > 1 } |
start learning | start learning |
Other data structures
| Question | Answer |
| create a map | m = [color:"Red", b:"Bar"] |
| access item in a map | m.color |
| define a property (in a class) | @Property myProp |
| dump object contents | o.dump() |
| overload the + operator | void plus( p ) { ... } |
start learning | start learning |
Files / IO
| Question | Answer |
| create a file | f = new File( "f.txt" ) |
| read text from file | f.getText() |
| write text to file | f.write( "hi" ) |
| run closure on directory structure | f.eachFileRecurse( { println it } ) |
| get text from a url | println new URL("http://java.net").text |
start learning | start learning |
Searching
| Question | Answer |
| check for regex match | "abc" =~ /c/ |
| check for regex match (whole string) | "ccc" ==~ /c+/ |
| find and display match | matcher = ( "abbc" =~ "(b+)" ); println matcher[0][1] |
| search and replace | "abc".replaceAll( "b", "bee" ) |
| search and replace (case insensitive) | "bBb".replaceAll( "(?i)B", "bee" ) |
| search and replace (using groups) | "abbc".replaceAll( "(b+)", " \$1 " ) |
start learning | start learning |
Pages linking here (main versions)
Groovy
|
|
|
| |