Groovy - List知识点
1.java 方式2.groovy 方式。
·
一、初始化
1.java 方式
def list = new ArrayList()
2.groovy 方式
//生成空 list
def list = []
//定义普通list
def list = [55,22,33]
//定义 map 数组
def list = [[score:65,sex:1,name:'张一'],[score:43,sex:2,name:'张二'][score:85,sex:1,name:'张三']]
二、获取元素
def ws = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]
//List[index] 可以使用负数索引
println ws[0] //1
//List.getAt(index) 可以使用负数索引
println ws.getAt(-1) //28
//List.get(index) 不可以使用负数索引
println ws.get(1) //3
三 、添加元素
1.List << x :将x 添加到List 中
def ws = [1, 3, 9]
ws << 'x'
println ws //[1, 3, 9, x]
ws = [1, 3, 9]
ws << 27 << 19
println ws //[1, 3, 9, 27, 19]
ws = [1, 3, 9]
ws << [27,19]
println ws //[1, 3, 9, [27, 19]]
2.List.leftShift(x):将x 添加到List 中,等价于 <<
def ws = [1, 3, 9]
ws.leftShift(27)
println ws // [1, 3, 9, 27]
ws = [1, 3, 9]
ws.leftShift([27,19])
println ws //[1, 3, 9, [27, 19]]
3.List.add([index],x):将x插入到List[指定索引位置]中
def ws = [1, 3, 9]
ws.add(27)
println ws
//[1, 3, 9, 27]
ws = [1, 3, 9]
ws.add(1,'e')
println ws
//输出结果 插入到指定索引位置
[1, e, 3, 9]
4.List.addAll([index],List2) 将List2插入到List[指定索引位置]中
def ws = [1, 3, 9]
ws.addAll([27,19])
println ws
//输出结果
[1, 3, 9, 27, 19]
ws = [1, 3, 9]
ws.addAll(1,[27,19])
println ws
//输出结果,插入到指定索引
[1, 27, 19, 3, 9]
5.List + x:“+” 号操作符,合并List,将 x 添加 List 中,返回一个新的 List
def ws = [1, 3, 9]
ws+= 27
println ws [1, 3, 9, 27]
ws = [1, 3, 9]
ws+=27 + 19
println ws
//输出结果,会直接相加
[1, 3, 9, 46]
ws = [1, 3, 9]
ws+=[27,19]+[26,16]
println ws
//输出结果
[1, 3, 9, 27, 19, 26, 16]
ws = [1, 3, 9]
ws+=[27,19]+ 26 + 16 + [17,20]
println ws
//输出结果
[1, 3, 9, 27, 19, 26, 16, 17, 20]
注意:合并多个时,第一个必须时List,否则会报错或者直接形成字符串的拼接
ws+=12+[27,19]+ 26 + 16 + [17,20] //报错
ws+='d'+[27,19]+ 26 + 16 + [17,20]
println ws
//输出结果
[1, 3, 9, d[27, 19]2616[17, 20]] //拼接
6.List.plus(x):等同于List+x
def ws = [1, 3, 9]
ws = ws.plus(27).plus(['w','e']).plus([7.4]).plus(54).plus('name')
println ws
//输出结果
[1, 3, 9, 27, w, e, 7.4, 54, name]
7.List[i] = x:将x插入到List中索引为i的位置,根据需要使列表增长,空值为null
def ws = [1, 3, 9]
ws[3] = 'e'
println ws //[1, 3, 9, e]
ws = [1, 3, 9]
ws[5] = 'e'
println ws //[1, 3, 9, null, null, e]
四、删除元素
1.List - x / List - List2:删除List中所有x元素/所有List2中的元素
def ws = ['a','b','c','a','d','b','ee']
ws-='a'
println ws
//输出结果
[b, c, d, b, ee]
ws = ['a','b','c','a','d','b','ee']
ws-=['a','b','ee']
println ws
//输出结果
[c, d]
2.remove:
List.remove(index) 根据下表删除List
List.remove("val") 根据指定值删除List
3.List.clear() 删除所有元素
五、遍历
1.使用each 方法
def list = [[name:"张三",score:60],[name:"李四",score:57]]
list.each{
//it是对应当前元素的隐式参数
println it.name
}
//
张三
李四
list.each{item->
println item.name
}
//
张三
李四
list.collect{
println it.name
}
//
张三
李四
2.带索引的遍历 eachWithIndex方法
def list = [[name:"张三",score:60],[name:"李四",score:57]]
list.eachWithIndex{item,index->
println "index 为 ${index},name 为 ${item.name}"
}
//
index 为 0,name 为 张三
index 为 1,name 为 李四
六、查找
1.find 查询满足指定条件的第一个元素
def list = [[name:"张三",score:60],[name:"李四",score:80]]
def result = list.find{
it.score>=60
}
println result
//[name:张三, score:60]
2.findAll 查询满足指定条件的所有元素
def list = [[name:"张三",score:60],[name:"李四",score:80]]
def result = list.findAll{
it.score>=60
}
println result
//[[name:张三, score:60], [name:李四, score:80]]
3.List.findIndexOf{条件}:找到第一个符合条件元素的索引
def list = [[name:"张三",score:60],[name:"李四",score:80]]
def result = list.findIndexOf{
it.score>70
}
println result
//1
4.List.indexOf(条件):返回第一个符合条件的索引
def list = ["a","b","a","d"]
def result = list.indexOf("a")
println result
// 0
//返回最后一个符合条件的索引
def result = list.lastIndexOf("a")
println result
//2
5.List.every{条件} 如果List中每个元素都满足就返回true,否则返回false
def list = [[name:"张三",score:60],[name:"李四",score:80]]
//def list = ["a","b","a","d"]
def result = list.every{
it.score>59
}
println result
//true
6.List.any{条件} 如果List中有一个满足就返回true,否值返回false
def list = [[name:"张三",score:60],[name:"李四",score:80]]
//def list = ["a","b","a","d"]
def result = list.any{
it.score>60
}
println result
//true
七、函数
1.sum 所有元素相加
def list = ["a","b","a","d"]
def result = list.sum{
it=='a'?1:it=='b'? 2:it=='d'?3:0
}
println result
def sumlist = [1,2,3,4]
println sumlist.sum()
//7
//10
2.List.join(分隔符) 将List 以分隔符连接,返回字符串
def list = ["a","b","a","d"]
println list.join('-')
//a-b-a-d
3.List.inject(){}将List以指定条件和初始值进行连接
def list = ["a","b","a","d"]
println list.inject("count:"){str,item->
str+item
}
//count:abad
4.极值List.max(),List.min()
// 最大值
println([9,4,2,10,5].max()) //10
// 最小值
println([9,4,2,10,5].min()) //2
// 单字符最大值和最小值
println(['x','y','a','z'].min()) //a
// 用闭包描述元素的大小
def list = ['abc','z','xyzuvw','hello','321']
println(list.max{it.size()}) //xyzuvw
println(list.min{it.size()}) //z
5.x in List:如果x在List中返回true
6.List.contains(x):如果List包含x返回true
7.List1.containsAll([List2]):如果List2中的每个元素都在List1中返回true
8.List.count(x):判断x在List中的数量
9.List.count{条件}:判断List中符合条件的元素的数量
10.List1.intersect(List2):判断List1和List2的交集
11.List1.disjoint(List2):如果List1和List2互斥返回true
12.*操作符
def collect = [
[name:'24f4',tel:"1","age":12],
[name:'e5t34',tel:"344","age":11],
[name:'rg43g',tel:"133","age":34],
[name:'2kj5',tel:"2334","age":52],
[name:'7e34',tel:"16823","age":13],
[name:'qe4r',tel:"115678","age":123],
null
]
//遍历集合key 为name 的所有值,为null 则输出null
collect*.name.each{
println it
}
//结果输出
24f4
e5t34
rg43g
2kj5
7e34
qe4r
null
//判断集合中name的值是否匹配,可包含空值
println collect*.name == ["24f4","e5t34","rg43g","2kj5","7e34","qe4r",null] //true
//判断集合中name的值是否匹配,必须完全匹配
println collect.name == ["24f4","e5t34","rg43g","2kj5","7e34","qe4r",null] //false
八、排序
List.sort():返回List从小到大排序后的结果
List.sort{条件}:将List按照条件排序
Collections.sort(List):将List进行排序
Collections.sort(List,闭包):将List通过闭包进行排序
println([6, 3, 9, 2, 7, 1, 5].sort())
println(['abc', 'z', 'xyzuvw', 'Hello', '321'].sort { it.size() })
println([7, 4, -6, -1, 11, 2, 3, -9, 5, -13].sort {
a, b -> a == b ? 0 : Math.abs(a) < Math.abs(b) ? -1 : 1
})
def list = [6, -3, 9, 2, -7, 1, 5]
Collections.sort(list)
println list
def mc = {
a, b -> a == b ? 0 : Math.abs(a) < Math.abs(b) ? -1 : 1
}
Collections.sort(list, mc)
println list
九、复制元素
List*n:将List复制n倍List.multiply(n):将List复制n倍Collections.nCopies(n,'x'):将x复制为n倍
十、分组
List.groupBy{条件}:将List按照条件分组
def list = [
[ "text": "6854-1-手术及急救装置", "value": "Ⅲ-6854-1-new"],
[ "text": "01有源手术器械", "value": "Ⅰ-1-1"],
[ "text": "6802-1-显微外科用刀", "value": "Ⅰ-6802-1-new"],
[ "text": "02无源手术器械", "value": "Ⅱ-2-2"],
[ "text": "6846-1-植入器材", "value": "Ⅲ-6846-1-new"],
[ "text": "03神经和心血管手术器械", "value": "Ⅲ-3-3"],
[ "text": "6815--注射穿刺器械","value": "Ⅱ-6815--new"],
[ "text": "06医用成像器械", "value": "Ⅲ-3-6"],
[ "text": "6801-1-基础外科用刀", "value": "Ⅰ-6801-1-new"],
[ "text": "6812--妇产科用手术器械","value": "Ⅱ-6812--new"]
]
def list2 = list.groupBy{
if(it.value.contains("new")){
if(it.value.contains("Ⅰ")){
['旧类-Ⅰ类']
}else if(it.value.contains("Ⅱ")){
['旧类-Ⅱ类']
}else if(it.value.contains("Ⅲ")){
['旧类-Ⅲ类']
}else{
['其他']
}
}else{
if(it.value.contains("Ⅰ")){
['新类-Ⅰ类']
}else if(it.value.contains("Ⅱ")){
['新类-Ⅱ类']
}else if(it.value.contains("Ⅲ")){
['新类-Ⅲ类']
}else{
['其他']
}
}
}
println list2
//输出结果
[
[旧类-Ⅲ类]:[
[text:6854-1-手术及急救装置, value:Ⅲ-6854-1-new],
[text:6846-1-植入器材, value:Ⅲ-6846-1-new]
],
[新类-Ⅰ类]:[
[text:01有源手术器械, value:Ⅰ-1-1]
],
[旧类-Ⅰ类]:[
[text:6802-1-显微外科用刀, value:Ⅰ-6802-1-new],
[text:6801-1-基础外科用刀, value:Ⅰ-6801-1-new]
],
[新类-Ⅱ类]:[
[text:02无源手术器械, value:Ⅱ-2-2]
],
[新类-Ⅲ类]:[
[text:03神经和心血管手术器械, value:Ⅲ-3-3],
[text:06医用成像器械, value:Ⅲ-3-6]
],
[旧类-Ⅱ类]:[
[text:6815--注射穿刺器械, value:Ⅱ-6815--new],
[text:6812--妇产科用手术器械, value:Ⅱ-6812--new]
]
]
更多推荐


所有评论(0)