我是靠谱客的博主 缓慢早晨,这篇文章主要介绍groovy 数据结构-映射1 groovy映射,现在分享给大家,希望可以做个参考。

1 groovy映射

1.2 map定义

groovy中映射定义如下:

复制代码
1
2
3
4
def colors = [         red : 'ff0000',         green : '00ff00',         blue : '0000ff']

这和列表方式定义一样,使用[]进行定义,不同的是在[]中Map是以key-value的形式给出没给元素的初始值。

1.3 索引方式

groovy提供了下标操作符的方式获取对应元素的值:

复制代码
1
println colors['red']

输出结果:

复制代码
1
ff000000

也可以通过以下方式获取map对应元素的值:

复制代码
1
println colors.green

输出结果:

复制代码
1
00ff00

1.4 添加元素

groovy可以直接通过map.key=value的方式进行添加元素

复制代码
1
2
colors.yellow = 'ffff00' println colors.yellow

输出结果:

复制代码
1
ffff00

map既可以添加同类型的元素,也可以添加不同类型的元素,这个是groovy中map比较强大的特性:

复制代码
1
2
colors.complex = [a : 1, b : 2] println colors.toMapString()

输出结果:

复制代码
1
[red:ff0000, green:00ff00, blue:0000ff, yellow:ffff00, complex:[a:1, b:2]]

注意问题:
1.  map中的key的类型一般是不可变字符串或者是Number。
2.  key不用单引号和有单引号效果相同,没有单引号编译器会默认使用单引号字符串。
groovy定义的map默认是LinkedHashMap,如果不想使用LinkedHashMap,可以在定义的时候指定类型,或者使用关键字as指定类型。

复制代码
1
2
HashMap colors = [] def colors = [] as HashMap

获取map类型时需要通过getClass()方法进行获取,不能直接通过class方法获取,因为通过class方法获取会被指定为在map中查找class的值。

1.5 删除元素

groovy中map删除元素使用remove()方法:

复制代码
1
2
3
4
5
6
def colors = [         red : 'ff0000',         green : '00ff00',         blue : '0000ff'] colors.remove('red') println colors.toMapString()

输出结果:

复制代码
1
[green:00ff00, blue:0000ff]

1.6 map常用操作

首先定义一个一个map如下:

复制代码
1
2
3
4
5
6
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()方法进行遍历:

复制代码
1
2
3
4
students.each { def student ->     println "the key is ${student.key}, " +             "the value is ${student.value}" }

输出结果:

复制代码
1
2
3
4
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参数:

复制代码
1
2
3
4
students.eachWithIndex { def student, int index ->     println "index is ${index}, the key is ${student.key}, " +             "the value is ${student.value}" }

输出结果:

复制代码
1
2
3
4
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的方法进行遍历:

复制代码
1
2
3
4
students.each { key, value ->     println "the key is ${key}, " +             "the value is ${value}" }

输出结果:

复制代码
1
2
3
4
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:

复制代码
1
2
3
4
students.eachWithIndex { key, value, index ->     println "this index is ${index}, the key is ${key}, " +             "the value is ${value}" }

输出结果:

复制代码
1
2
3
4
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()方法进行查找:

复制代码
1
2
3
4
def entry = students.find { def student ->     return student.value.score >= 60 } println entry

输出结果:

复制代码
1
2={number=0002, name=Johnny, score=60, sex=female}

使用findAll查找全部满足条件的数据:

复制代码
1
2
3
4
def entrys = students.findAll { def student ->     return student.value.score >= 60 } println entrys

输出结果:

复制代码
1
2
[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()进行数据统计:

复制代码
1
2
3
4
5
def count = students.count { def student ->     return student.value.score >= 60 &&             student.value.sex == 'male' } println count

输出结果:

复制代码
1
1

查找所有score大于等于60的姓名:

复制代码
1
2
3
4
5
6
def names = students.findAll { def student ->     return student.value.score >= 60 }.collect {     return it.value.name } println names.toListString()

输出结果:

复制代码
1
[Johnny, Claire, Amy]

1.6.4 分组

使用groupBy对map中元素进行分组:

复制代码
1
2
3
4
def group = students.groupBy { def student ->     return student.value.score >= 60 ? '及格': '不及格' } println group.toMapString()

输出结果:

复制代码
1
2
[不及格:[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中的元素进行排序。

复制代码
1
2
3
4
5
6
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
2
[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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部