1. encode

def str = URLEncoder.encode 'a b中国', 'UTF-8';

 2.decode

def original = URLDecoder.decode str ,'UTF-8';

 3.  定义

def groovyBook = new BookBean()

groovyBook.title = 'Groovy conquers the world'

 4. Grab 

@Grab('commons-lang:commons-lang:2.4')
import org.apache.commons.lang.ClassUtils

5. list

def roman = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII']
assert roman[4] == 'IV'
roman[8] = 'VIII'
assert roman.size() == 9

List longList = (0..1000).toList()

myList = ['a','b','c','d','e','f']
assert myList[0..2] == ['a','b','c']
assert myList[0,2,4] == ['a','c','e']
myList[0..2] = ['x','y','z']
assert myList == ['x','y','z','d','e','f']
myList[3..5] = []
assert myList == ['x','y','z']
myList[1..1] = [0, 1, 2]
assert myList == ['x', 0, 1, 2, 'z']


def list = [1, 2, 3]
assert list.first() == 1
assert list.head() == 1
assert list.tail() == [2, 3]

assert list.last() == 3
assert list.count(2) == 1
assert list.max() == 3
assert list.min() == 1
def even = list.find { item ->
item % 2 == 0
}

6. map

def http = [
100 : 'CONTINUE',
200 : 'OK',
400 : 'BAD REQUEST'
]
assert http[200] == 'OK'
http[500] = 'INTERNAL SERVER ERROR'
assert http.size() == 4

7. ranges

def x = 1..10
assert x.contains(5)
assert !x.contains(15)
assert x.size() == 10
assert x.from == 1
assert x.to == 10
assert x.reverse() == 10..1

8. upto

def totalClinks = 0
def partyPeople = 100
1.upto(partyPeople) { guestNumber ->
clinksWithGuest = guestNumber-1
totalClinks += clinksWithGuest
}

9. for in

def list = [1,2,3,4,5]

for(i in list){
print i
}


10. 容器size


Type Determine the size in JDK via ... Groovy
Array length field size() method
Array java.lang.reflect.Array.getLength(array) size() method
String length() method size() method
StringBuffer length() method size() method
Collection size() method size() method
Map size() method size() method
File length() method size() method
Matcher groupCount() method size() method


11. operators

groovy 函数1_apache

groovy 函数1_apache_02