一、初始化
1.java 方式
def map = new HashMap()
2.groovy 方式
map的键必须指定为不可变的单引号字符串,如果没有给键指定单引号,编译器编译时会自动加上(就是键直接写值,而不用加上单引号)
//生成空map
def map = [:]
// 初始化map,键可以不加单引号,默认 LinkedHashMap 类型
def map= [red: '红色', green: '绿色', 'blue': '蓝色']
//指定类型
def map= [red: '红色', green: '绿色', 'blue': '蓝色']as Hashtable
HashMap map= [red: '红色', green: '绿色', 'blue': '蓝色']
二、获取元素
//使用getAt方法获取
map.getAt("key")
//使用get方法获取
map.get("key")
//当map中key 存在时 不会修改map,当key 不存在时会添加key的值,并返回添加的value
map.get("key","999")
//使用变量名[“键”]
map['key']
//使用变量名.键名
map.key
三 、添加元素
//put 方法
map.put("key","value")
//变量名.键名=值
map.key = value
//leftShift 方法
map.leftShift(["key":"value"])
// “+” 号操作符重载
def map = ["a":1, "b":2]
map+=["c":3]
println map
//输出结果 [a:1, b:2, c:3]
四、删除元素
map.remove("key")
//使用remove方法,该方法有两个重载方法,第一个重载方法只需要一个参数,就是集合的键;第二个重载方法需要两个参数,分别为key与value,根据键值对删除集合中的元素
map.remove("key","value")
// “-” 号操作符重载
def map = ["a":1, "b":2]
map-=["a":1]
println map
//输出结果 [b:2]
五、遍历
1.使用each 方法
def map = [red: '红色', green: '绿色', 'blue': '蓝色']
map.each {item ->
println "the key is ${item.key} and the value is ${item.value}"
}
//根据key与value进行遍历,参数顺序不能颠倒
map.each {key,value ->
println "the key is ${key} and the value is ${value}"
}
//输入结果
the key is red and the value is 红色
the key is green and the value is 绿色
the key is blue and the value is 蓝色
2.带索引的遍历 eachWithIndex方法
def map = [red: '红色', green: '绿色', 'blue': '蓝色']
//参数顺序不能颠倒
map.eachWithIndex {item,index ->
println "the index is ${index} ,and the key is ${item.key} and the value is ${item.value}"
}
//根据key与value进行遍历,参数顺序不能颠倒
map.eachWithIndex {key,value,index ->
println "the index is ${index} ,and the key is ${key} and the value is ${value}"
}
map.eachWithIndex {def key,def value,int index ->
println "the index is ${index} ,and the key is ${key} and the value is ${value}"
}
//输出结果
the index is 0 ,and the key is red and the value is 红色
the index is 1 ,and the key is green and the value is 绿色
the index is 2 ,and the key is blue and the value is 蓝色
六、查找
1.find 查询满足指定条件的第一个元素
def map = [a:55,b:42,c:83,d:90,e:63,f:76]
def entry = map.find{item->
return item.value>60
}
println entry
//输入结果
c=83
entry.each{item->
println "this key is ${item.key},this value is ${item.value}"
}
//输入结果
this key is c,this value is 83
2.findAll 查询满足指定条件的所有元素
def map = [a:55,b:42,c:83,d:90,e:63,f:76]
def entry = map.findAll{item->
return item.value>60
}
println entry
//输出结果
[c:83, d:90, e:63, f:76]
entry.each{item->
println "this key is ${item.key},this value is ${item.value}"
}
//输出结果
this key is c,this value is 83
this key is d,this value is 90
this key is e,this value is 63
this key is f,this value is 76
3.count 统计满足指定条件的个数,返回值为Integer
def map = [a:[score:78,sex:"女",age:12,name:"张一"],b:[score:76,sex:"男",age:13,name:"张二"],c:[score:99,sex:"女",age:11,name:"张三"],d:[score:34,sex:"男",age:10,name:"张四"],e:[score:45,sex:"男",age:12,name:"张五"]]
def count = map.count{item->
return item.value.score>60 && item.value.sex=="女"
}
println count
//输出结果
2
4.collect 对集合进行过滤,查询集合每个元素是否满足指定条件,返回每个元素的 boolean 组成的数组
def map = [a:[score:54,sex:"女",age:12,name:"张一"],b:[score:76,sex:"女",age:13,name:"张二"],c:[score:99,sex:"男",age:11,name:"张三"],d:[score:64,sex:"男",age:10,name:"张四"],e:[score:61,sex:"女",age:12,name:"张五"]]
def entry = map.collect{item->
return item.value.score>60 && item.value.sex=="男"
}
println entry
//输出结果
[false, false, true, true, false]
entry.each{item->
println "this value is ${item}"
}
//输出结果
this value is false
this value is false
this value is true
this value is true
this value is false
5.groupBy 借助闭包,根据指定条件对Map集合进行分组
def map = [a:[score:54,sex:"女",age:12,name:"张一"],b:[score:76,sex:"女",age:13,name:"张二"],c:[score:99,sex:"男",age:11,name:"张三"],d:[score:64,sex:"男",age:10,name:"张四"],e:[score:61,sex:"女",age:12,name:"张五"]]
def entry = map.groupBy{item->
return item.value.score>60?"及格":"不及格"
}
println entry['及格']
//输出结果
[b:[score:76, sex:女, age:13, name:张二], c:[score:99, sex:男, age:11, name:张三], d:[score:64, sex:男, age:10, name:张四], e:[score:61, sex:女, age:12, name:张五]]
entry['及格'].each{item->
println "this value is ${item.value}"
}
//输出结果
this value is [score:76, sex:女, age:13, name:张二]
this value is [score:99, sex:男, age:11, name:张三]
this value is [score:64, sex:男, age:10, name:张四]
this value is [score:61, sex:女, age:12, name:张五]
println entry['不及格']
//输出结果
[a:[score:54, sex:女, age:12, name:张一]]
6.sort 根据指定条件对Map集合进行排序
def map = [a:[score:54,sex:"女",age:12,name:"张一"],b:[score:76,sex:"女",age:13,name:"张二"],c:[score:99,sex:"男",age:11,name:"张三"],d:[score:64,sex:"男",age:10,name:"张四"],e:[score:54,sex:"女",age:12,name:"张五"]]
def entry = map.sort{item1,item2->
def score1 = item1.value.score
def score2 = item2.value.score
return score1==score2 ? 0 : score1 < score2 ? 1 : -1 //倒序
// return score1==score2 ? 0 : score1 < score2 ? -1 : 1 //顺序
}
println entry
//输出结果
[c:[score:99, sex:男, age:11, name:张三], b:[score:76, sex:女, age:13, name:张二], d:[score:64, sex:男, age:10, name:张四], a:[score:54, sex:女, age:12, name:张一], e:[score:54, sex:女, age:12, name:张五]]
entry.each{item->
println "this value is ${item.value}"
}
//输出结果
this value is [score:99, sex:男, age:11, name:张三]
this value is [score:76, sex:女, age:13, name:张二]
this value is [score:64, sex:男, age:10, name:张四]
this value is [score:54, sex:女, age:12, name:张一]
this value is [score:54, sex:女, age:12, name:张五]
7.判断 map 中是否满足指定条件,返回 boolean
def map = [a:[score:54,sex:"女",age:12,name:"张一"],b:[score:76,sex:"女",age:13,name:"张二"],c:[score:99,sex:"男",age:11,name:"张三"],d:[score:64,sex:"男",age:10,name:"张四"],e:[score:54,sex:"女",age:12,name:"张五"]]
def flg = map.any{item->
item.value.score<90
}
println flg //true
8.判断 map 中 每个元素是否都满足指定条件,返回 boolean
def map = [a:[score:54,sex:"女",age:12,name:"张一"],b:[score:76,sex:"女",age:13,name:"张二"],c:[score:99,sex:"男",age:11,name:"张三"],d:[score:64,sex:"男",age:10,name:"张四"],e:[score:54,sex:"女",age:12,name:"张五"]]
def flg = map.every{item->
item.value.score>60
}
println flg //false
七、函数
1.判断 map 中是否包含指定 key
def map = ["a":1, "b":2]
println map.containsKey("a") //true
println map.containsKey("c") //false
2.获取 map 中的所有的 key值列表
def map = ["a":1, "b":2]
println map.keySet()
//输出结果
[a, b]
3.将 map 的所有值变为 List
def map = ["a":1, "b":2]
println map.values().asList()
//输出结果
[1, 2]
4.* 展开操作符,获取 map 所有值 或者 所有 key 为 List
def map = ["a":1, "b":2]
println map*.getValue() //[1, 2]
println map*.getKey() //[a, b]
5.通过原来 map 中的 键值将其键值对 放入 一个新的 map 中
def map = [a:[score:54,sex:"女",age:12,name:"张一"],b:[score:76,sex:"女",age:13,name:"张二"],c:[score:99,sex:"男",age:11,name:"张三"],d:[score:64,sex:"男",age:10,name:"张四"],e:[score:54,sex:"女",age:12,name:"张五"]]
def newmap = map.subMap(['a'])
println newmap
//输出结果
[a:[score:54, sex:女, age:12, name:张一]]