2019独角兽企业重金招聘Python工程师标准>>>
Map是使用键值对表示,表现形式如下
复制代码
1
2
3<!-- lang: groovy --> def a = [key1:value1,key2:value2]
操作Map
复制代码
1
2
3
4<!-- lang: groovy --> def map = ['name':'Bashar','age':26,skills:['java','groovy'],'author':true] assert map.size() == 4
添加K/V值到Map
复制代码
1
2
3
4
5
6
7
8
9<!-- lang: groovy --> map += ['city':'Tucson'] assert map == ['name':'Bashar','age':26,skills:['java','groovy'],'author':true,'city':'Tucson'] <!-- lang: groovy --> map['state'] = 'AZ' assert map == ['name':'Bashar','age':26,skills:['java','groovy'],'author':true,'city':'Tucson','state':'AZ']
访问Map中的元素
复制代码
1
2
3
4
5
6
7<!-- lang: groovy --> assert map.city == 'Tucson' assert map['city'] == 'Tucson' assert map.get('city') == 'Tucson' assert map.getAt('city') == 'Tucson' assert map.skills[0] == 'Java'
Map的键是唯一的
复制代码
1
2
3<!-- lang: groovy --> assert ['name':'Bashar','name':'Abdul'] == ['name':'Abdul']
迭代Map
复制代码
1
2
3
4
5
6
7
8
9<!-- lang: groovy --> map.each{ it-> println it.key + ":" + it.value } map.eachWithIndex{ it,index -> println "item $index - " + it.key + ":" + it.value }
转载于:https://my.oschina.net/65304586/blog/165628
最后
以上就是清脆柠檬最近收集整理的关于Groovy&Grails-技术问答-集合数据类型Map的全部内容,更多相关Groovy&Grails-技术问答-集合数据类型Map内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复