Groovy提供了许多‘匕首方法’(匕首,短小精悍也~如each,eachWithIndex, any,every,grep,join,sort,find,findAll,collect,groupBy,inject,reverse, tokenize, unique,max,min,count,sum等)来提升开发者的开发效率,但常被Java开发人员忽视。在这篇随笔中我将为您演示各方法的使用。

each

遍历list

def list = ['a', 'b', 'c']
list.each { elem ->
println elem
}

运行结果:
a
b
c

遍历map

def map = [name:"山风小子", address:"Shanghai"]
map.each { key, value ->
println "$key : $value"
}

运行结果:

name : 山风小子
address : Shanghai

eachWithIndex

带index的each

def list = ['a', 'b', 'c']
list.eachWithIndex { elem, i ->
println "$i : $elem"
}

运行结果:
0 : a
1 : b
2 : c

any

只要存在一个满足条件(此例中的条件为elem.length() < 3)的element就返回true,否则返回false

def list = ['a', 'ab', 'abc']
list.any { elem ->
elem.length() < 3
}

运行结果:
true

every

所有的element都满足条件才返回true,否则返回false

def list = ['a', 'ab', 'abc']
list.every { elem ->
elem.length() < 3
}

运行结果:
false

grep

符合条件的element会被提取出来,形成一个list

条件以closure的形式传入

def list = ['a', 'ab', 'abc']
list.grep { elem ->
elem.length() < 3
}

运行结果:
["a", "ab"]

条件以regex的形式传入,符合regex的element被提取出来形成一个list

def list = ['a', 'ab', 'abc']
list.grep(~/../)

运行结果:
["ab"]

条件以collection的形式传入,在collection中的element被提取出来形成一个list,可以看作求两个collection的交集

def list = ['a', 'ab', 'abc']
list.grep(['a', 'cde', 'ab'])

运行结果:
["a", "ab"]

join

用指定的字符连接collection中的element

def list = [2007, 8, 26]
list.join('-')

运行结果:
2007-8-26

sort

根据指定条件进行排序

def list = [2007, 8, 26]
list.sort { e1, e2 ->
return e1 - e2
}

运行结果:
[8, 26, 2007]

find

查找collection中满足条件的‘第一个’element

def list = [2007, 8, 26]
list.find { elem ->
elem <30
}

运行结果:
8

findAll

查找collection中满足条件的‘所有’element

def list = [2007, 8, 26]
list.findAll { elem ->
elem < 30
}

运行结果:
[8, 26]

collect

对collection的element进行处理,并将处理结果放到一个新的collection中

def list = ['a', 'b', 'c']
list.collect { elem ->
elem * 2
}

运行结果:
["aa", "bb", "cc"]

对map进行处理

def map = [name:'山风小子', address:'Shanghai']
map.collect { entry ->
"${entry.key} : ${entry.value}"
}

运行结果:
[name : 山风小子, address : Shanghai]
groupBy

对collection中的element按给定条件进行分组

def list = ['a', 'b', 'abc', 'ab', 'c', 'bc']
list.groupBy { elem ->
elem.length()
}

运行结果:
[1:["a", "b", "c"], 2:["ab", "bc"], 3:["abc"]]
inject

一个累积的过程,传入inject方法的'I'作为sum的初始值,在遍历collection的过程中,将处理结果("$sum $elem ")保存到sum中

def list = ["love", "you"]
list.inject('I') { sum, elem ->
"$sum $elem "
}

运行结果:
I love  you

reverse

将collection中各element的次序颠倒一下

def list = ['a', 'b', 'c']
list.reverse()

运行结果:
["c", "b", "a"]

颠倒字符串

def list = 'abc'
list.reverse()

运行结果:
cba

tokenize

指定分隔符,取得token集

'a1/b2/c3/d4'.tokenize('/')

'a1/b2/c3/d4'.tokenize('/')
运行结果:
["a1", "b2", "c3", "d4"]

unique

去除collection中重复的element

def list = ['a', 'b', 'b', 'a', 'c']
list.unique()

运行结果:
["a", "b", "c"]
max

求最大值

def list = [1, 2, 3]
list.max()

运行结果:
3

按指定的比较内容(此例的比较内容为长度length),在collection中选出最大的element

def list = ['a', 'ab', 'abc']
list.max { elem ->
elem.length()
}

min与max类似,求最小值,再次就不演示用法了,用法与max相同,将上述代码中的max改为min即可

count

计数

def list = ['a', 'ab', 'a']
list.count('a')

运行结果:
2

对字符串"aaba"中的a进行计数

def list = "aaba"
list.count('a')

运行结果:
3

sum

求和

def list = [1, 2, 3]
list.sum()

运行结果:
6

求字符串的‘和’,其实就是连接字符串

def list = ['a', 'b', 'c']
list.sum()

运行结果:
abc

最后,我想提醒各位一点:有时您可以将string看作list,对string使用适用于list的‘匕首方法’。