Groovy高效编程——‘匕首方法’的使用
来源:互联网 发布:优化诊断zhangzifan 编辑:程序博客网 时间:2024/05/16 09:40
Groovy提供了许多‘匕首方法’(匕首,短小精悍也~如each,eachWithIndex,any,every,grep,join,sort,find,findAll,collect,groupBy,inject,reverse,tokenize, unique,max,min,count,sum等)来提升开发者的开发效率,但常被Java开发人员忽视。在这篇随笔中我将为您演示各方法的使用。
each
遍历list
遍历map
eachWithIndex
带index的each
any
只要存在一个满足条件(此例中的条件为elem.length() < 3)的element就返回true,否则返回false
every
所有的element都满足条件才返回true,否则返回false
grep
符合条件的element会被提取出来,形成一个list
条件以closure的形式传入
条件以regex的形式传入,符合regex的element被提取出来形成一个list
条件以collection的形式传入,在collection中的element被提取出来形成一个list,可以看作求两个collection的交集
join
用指定的字符连接collection中的element
sort
根据指定条件进行排序
find
查找collection中满足条件的‘第一个’element
findAll
查找collection中满足条件的‘所有’element
collect
对collection的element进行处理,并将处理结果放到一个新的collection中
对map进行处理
groupBy
对collection中的element按给定条件进行分组
inject
一个累积的过程,传入inject方法的'I'作为sum的初始值,在遍历collection的过程中,将处理结果("$sum $elem ")保存到sum中
reverse
将collection中各element的次序颠倒一下
颠倒字符串
tokenize
指定分隔符,取得token集
unique
去除collection中重复的element
max
求最大值
按指定的比较内容(此例的比较内容为长度length),在collection中选出最大的element
min与max类似,求最小值,再次就不演示用法了,用法与max相同,将上述代码中的max改为min即可
count
计数
对字符串"aaba"中的a进行计数
sum
求和
求字符串的‘和’,其实就是连接字符串
each
遍历list
def list = ['a', 'b', 'c']
list.each { elem ->
println elem
}
运行结果:list.each { elem ->
println elem
}
a
b
c
b
c
遍历map
def map = [name:"山风小子", address:"Shanghai"]
map.each { key, value ->
println "$key : $value"
}
运行结果:map.each { key, value ->
println "$key : $value"
}
name : 山风小子
address : Shanghai
address : Shanghai
eachWithIndex
带index的each
def list = ['a', 'b', 'c']
list.eachWithIndex { elem, i ->
println "$i : $elem"
}
运行结果:list.eachWithIndex { elem, i ->
println "$i : $elem"
}
0 : a
1 : b
2 : c
1 : b
2 : c
any
只要存在一个满足条件(此例中的条件为elem.length() < 3)的element就返回true,否则返回false
def list = ['a', 'ab', 'abc']
list.any { elem ->
elem.length() < 3
}
运行结果:list.any { elem ->
elem.length() < 3
}
true
every
所有的element都满足条件才返回true,否则返回false
def list = ['a', 'ab', 'abc']
list.every { elem ->
elem.length() < 3
}
运行结果:list.every { elem ->
elem.length() < 3
}
false
grep
符合条件的element会被提取出来,形成一个list
条件以closure的形式传入
def list = ['a', 'ab', 'abc']
list.grep { elem ->
elem.length() < 3
}
运行结果:list.grep { elem ->
elem.length() < 3
}
["a", "ab"]
条件以regex的形式传入,符合regex的element被提取出来形成一个list
def list = ['a', 'ab', 'abc']
list.grep(~/../)
运行结果:list.grep(~/../)
["ab"]
条件以collection的形式传入,在collection中的element被提取出来形成一个list,可以看作求两个collection的交集
def list = ['a', 'ab', 'abc']
list.grep(['a', 'cde', 'ab'])
运行结果:list.grep(['a', 'cde', 'ab'])
["a", "ab"]
join
用指定的字符连接collection中的element
def list = [2007, 8, 26]
list.join('-')
运行结果:list.join('-')
2007-8-26
sort
根据指定条件进行排序
def list = [2007, 8, 26]
list.sort { e1, e2 ->
return e1 - e2
}
运行结果:list.sort { e1, e2 ->
return e1 - e2
}
[8, 26, 2007]
find
查找collection中满足条件的‘第一个’element
def list = [2007, 8, 26]
list.find { elem ->
elem <30
}
运行结果:list.find { elem ->
elem <30
}
8
findAll
查找collection中满足条件的‘所有’element
def list = [2007, 8, 26]
list.findAll { elem ->
elem < 30
}
运行结果:list.findAll { elem ->
elem < 30
}
[8, 26]
collect
对collection的element进行处理,并将处理结果放到一个新的collection中
def list = ['a', 'b', 'c']
list.collect { elem ->
elem * 2
}
运行结果:list.collect { elem ->
elem * 2
}
["aa", "bb", "cc"]
对map进行处理
def map = [name:'山风小子', address:'Shanghai']
map.collect { entry ->
"${entry.key} : ${entry.value}"
}
运行结果: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()
}
运行结果: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 "
}
运行结果:list.inject('I') { sum, elem ->
"$sum $elem "
}
I love you
reverse
将collection中各element的次序颠倒一下
def list = ['a', 'b', 'c']
list.reverse()
运行结果:list.reverse()
["c", "b", "a"]
颠倒字符串
def list = 'abc'
list.reverse()
运行结果:list.reverse()
cba
tokenize
指定分隔符,取得token集
'a1/b2/c3/d4'.tokenize('/')
运行结果:["a1", "b2", "c3", "d4"]
unique
去除collection中重复的element
def list = ['a', 'b', 'b', 'a', 'c']
list.unique()
运行结果:list.unique()
["a", "b", "c"]
max
求最大值
def list = [1, 2, 3]
list.max()
运行结果:list.max()
3
按指定的比较内容(此例的比较内容为长度length),在collection中选出最大的element
def list = ['a', 'ab', 'abc']
list.max { elem ->
elem.length()
}
list.max { elem ->
elem.length()
}
min与max类似,求最小值,再次就不演示用法了,用法与max相同,将上述代码中的max改为min即可
count
计数
def list = ['a', 'ab', 'a']
list.count('a')
运行结果:list.count('a')
2
对字符串"aaba"中的a进行计数
def list = "aaba"
list.count('a')
运行结果:list.count('a')
3
sum
求和
def list = [1, 2, 3]
list.sum()
运行结果:list.sum()
6
求字符串的‘和’,其实就是连接字符串
def list = ['a', 'b', 'c']
list.sum()
运行结果:list.sum()
abc
0 0
- Groovy高效编程——‘匕首方法’的使用
- Groovy高效编程——‘匕首方法’的使用
- Groovy高效编程——‘匕首方法’的使用
- Groovy高效编程——‘匕首方法’的使用
- Groovy高效编程——‘匕首方法’的使用
- Groovy高效编程——‘匕首方法’的使用
- Groovy高效编程——‘匕首方法’的使用
- Groovy高效编程——‘匕首方法'
- Groovy高效编程——‘匕首方法’的使用(更新于2007.09.14)(转载)
- groovy匕首方法
- Groovy高效编程——动态改变对象的能力
- Groovy高效编程——动态改变对象的能力
- Groovy高效编程——创建DSL
- Groovy高效编程——统计单词频率
- Groovy高效编程——生成XML文件
- Groovy高效编程——动态改变对象的能力(1)
- Groovy高效编程——动态改变对象的能力(2)
- Groovy高效编程——用Groovy轻松搞定SOAP消息
- Tomcat启动报错总结
- Node.js 模块 包
- java动态代理学习笔记
- android自动化测试 input
- objective-C 的内存管理之-自动释放池(autorelease pool)
- Groovy高效编程——‘匕首方法’的使用
- repo 用法
- struct1 与 struct2 的区别
- MFC自绘控件学习总结(1)
- jmail发送有链接的邮件
- UVA 10537 The Toll! Revisited(最短路变形+输出字典序最小路径)
- nginx配置path_info
- 必须收藏的14个响应式布局前端开发框架
- Java了解相对较多!