Create
Learn
Share

Groovy 2

rename
Updated 2007-01-14 00:24

More lists

Question Answer
merge 2 listsl + myOtherList
subtract listsl - myOtherList
loop 3 times3.times() { println it }
grep a listl.grep( /a/ )
find largest value (in a list)l.max()
find values above 1 (in a list)[1, 2, 3].findAll { it > 1 }
memorize

Other data structures

Question Answer
create a mapm = [color:"Red", b:"Bar"]
access item in a mapm.color
define a property (in a class)@Property myProp
dump object contentso.dump()
overload the + operatorvoid plus( p ) { ... }
memorize

Files / IO

Question Answer
create a filef = new File( "f.txt" )
read text from filef.getText()
write text to filef.write( "hi" )
run closure on directory structuref.eachFileRecurse( { println it } )
get text from a urlprintln new URL("http://java.net").text
memorize

Searching

Question Answer
check for regex match"abc" =~ /c/
check for regex match (whole string)"ccc" ==~ /c+/
find and display matchmatcher = ( "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 " )
memorize