概述
1 groovy映射
1.2 map定义
groovy中映射定义如下:
def colors = [
red : 'ff0000',
green : '00ff00',
blue : '0000ff']
这和列表方式定义一样,使用[]进行定义,不同的是在[]中Map是以key-value的形式给出没给元素的初始值。
1.3 索引方式
groovy提供了下标操作符的方式获取对应元素的值:
println colors['red']
输出结果:
ff000000
也可以通过以下方式获取map对应元素的值:
println colors.green
输出结果:
00ff00
1.4 添加元素
groovy可以直接通过map.key=value的方式进行添加元素
colors.yellow = 'ffff00'
println colors.yellow
输出结果:
ffff00
map既可以添加同类型的元素,也可以添加不同类型的元素,这个是groovy中map比较强大的特性:
colors.complex = [a : 1, b : 2]
println colors.toMapString()
输出结果:
[red:ff0000, green:00ff00, blue:0000ff, yellow:ffff00, complex:[a:1, b:2]]
注意问题:
1. map中的key的类型一般是不可变字符串或者是Number。
2. key不用单引号和有单引号效果相同,没有单引号编译器会默认使用单引号字符串。
groovy定义的map默认是LinkedHashMap,如果不想使用LinkedHashMap,可以在定义的时候指定类型,或者使用关键字as指定类型。
HashMap colors = []
def colors = [] as HashMap
获取map类型时需要通过getClass()方法进行获取,不能直接通过class方法获取,因为通过class方法获取会被指定为在map中查找class的值。
1.5 删除元素
groovy中map删除元素使用remove()方法:
def colors = [
red : 'ff0000',
green : '00ff00',
blue : '0000ff']
colors.remove('red')
println colors.toMapString()
输出结果:
[green:00ff00, blue:0000ff]
1.6 map常用操作
首先定义一个一个map如下:
def students = [
1 : [number : '0001', name : 'Bob', score : 55, sex : 'male'],
2 : [number : '0002', name : 'Johnny', score : 60, sex : 'female'],
3 : [number : '0003', name : 'Claire', score : 65, sex : 'female'],
4 : [number : '0004', name : 'Amy', score : 70, sex : 'male']
]
1.6.1 遍历
使用each()方法进行遍历:
students.each { def student ->
println "the key is ${student.key}, " +
"the value is ${student.value}"
}
输出结果:
the key is 1, the value is [number:0001, name:Bob, score:55, sex:male]
the key is 2, the value is [number:0002, name:Johnny, score:60, sex:female]
the key is 3, the value is [number:0003, name:Claire, score:65, sex:female]
the key is 4, the value is [number:0004, name:Amy, score:70, sex:male]
也可以使用eachWithIndex(),和each()不同的是,eachWithIndex()多了一个index参数:
students.eachWithIndex { def student, int index ->
println "index is ${index}, the key is ${student.key}, " +
"the value is ${student.value}"
}
输出结果:
index is 0, the key is 1, the value is [number:0001, name:Bob, score:55, sex:male]
index is 1, the key is 2, the value is [number:0002, name:Johnny, score:60, sex:female]
index is 2, the key is 3, the value is [number:0003, name:Claire, score:65, sex:female]
index is 3, the key is 4, the value is [number:0004, name:Amy, score:70, sex:male]
还可以使用each()方法,只不过闭包参数时key,value的方法进行遍历:
students.each { key, value ->
println "the key is ${key}, " +
"the value is ${value}"
}
输出结果:
the key is 1, the value is [number:0001, name:Bob, score:55, sex:male]
the key is 2, the value is [number:0002, name:Johnny, score:60, sex:female]
the key is 3, the value is [number:0003, name:Claire, score:65, sex:female]
the key is 4, the value is [number:0004, name:Amy, score:70, sex:male]
使用上面的方式也可以带第三个参数index:
students.eachWithIndex { key, value, index ->
println "this index is ${index}, the key is ${key}, " +
"the value is ${value}"
}
输出结果:
this index is 0, the key is 1, the value is [number:0001, name:Bob, score:55, sex:male]
this index is 1, the key is 2, the value is [number:0002, name:Johnny, score:60, sex:female]
this index is 2, the key is 3, the value is [number:0003, name:Claire, score:65, sex:female]
this index is 3, the key is 4, the value is [number:0004, name:Amy, score:70, sex:male]
1.6.2 查找
通过find()方法进行查找:
def entry = students.find { def student ->
return student.value.score >= 60
}
println entry
输出结果:
2={number=0002, name=Johnny, score=60, sex=female}
使用findAll查找全部满足条件的数据:
def entrys = students.findAll { def student ->
return student.value.score >= 60
}
println entrys
输出结果:
[2:[number:0002, name:Johnny, score:60, sex:female], 3:[number:0003, name:Claire, score:65,
sex:female], 4:[number:0004, name:Amy, score:70, sex:male]]
1.6.3 统计
使用count()进行数据统计:
def count = students.count { def student ->
return student.value.score >= 60 &&
student.value.sex == 'male'
}
println count
输出结果:
1
查找所有score大于等于60的姓名:
def names = students.findAll { def student ->
return student.value.score >= 60
}.collect {
return it.value.name
}
println names.toListString()
输出结果:
[Johnny, Claire, Amy]
1.6.4 分组
使用groupBy对map中元素进行分组:
def group = students.groupBy { def student ->
return student.value.score >= 60 ? '及格': '不及格'
}
println group.toMapString()
输出结果:
[不及格:[1:[number:0001, name:Bob, score:55, sex:male]], 及格:[2:[number:0002, name:Johnny,
score:60, sex:female], 3:[number:0003, name:Claire, score:65, sex:female], 4:[number:0004, name:Amy, score:70, sex:male]]]
1.6.5 排序
使用sort()方法对Map的元素进行排序,会返回一个新的Map,与List不同的是,List中的sort()方法只对List中的元素进行排序。
def sort = students.sort { def student1, def student2 ->
Number score1 = student1.value.score
Number score2 = student2.value.score
return score1 == score2 ? 0 : score1 < score2 ? -1 : 1
}
println sort.toMapString()
输出结果:
[1:[number:0001, name:Bob, score:55, sex:male], 2:[number:0002, name:Johnny, score:60,
sex:female], 3:[number:0003, name:Claire, score:65, sex:female], 4:[number:0004, name:Amy, score:70, sex:male]]
最后
以上就是缓慢早晨为你收集整理的groovy 数据结构-映射1 groovy映射的全部内容,希望文章能够帮你解决groovy 数据结构-映射1 groovy映射所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复