• Groovy之Map操作


    1、Map的生成方式

    方式一:Java方式

    def map = new HashMap() 

    方式二:Groovy方式

    def colors = ['red': '红色', 'green': '绿色', blue: '蓝色'] // 键可以不加单引号

    注意:map的键必须指定为不可变的单引号字符串,如果没有给键指定单引号,编译器编译时会自动加上(就是键直接写值,而不用加上单引号)

    2、更改Groovy默认map类型

    map默认的是LinkedHashMap,如果我们想要指定集合类型,可以将def关键字替换为自己想要的map类型,例如

    如果我想要指定为Hashtable

    HashMap colors = ['red': '红色', 'green': '绿色', 'blue': '蓝色']

    或者使用 as关键字,完成指定map类型

    def colors = ['red': '红色', 'green': '绿色', 'blue': '蓝色'] as Hashtable

    3、获取集合元素

    方式一:使用getAt方法获取

    colors.getAt("red")

    方式二:使用变量名[“键”]

    println colors['red']

    方式三:使用变量名.键名

    colors.red

    4、添加元素

    4.1、put方法

    colors.put("yellow", "黄色")

     4.2、变量名.键名=值

    colors.yellow = 'fff00' 

    4.3、leftShift方法

    colors.leftShift(["棕色": 'brown'])

    注意:传入的是一个map集合

    4.4、添加嵌套元素

    colors.complex = ["a": 3, "b": 5]

     注:在map集合中获取类属性必须使用getClass方法,而不能使用.class方式获取了

     

    5、删除元素

    使用remove方法,该方法有两个重载方法,第一个重载方法只需要一个参数,就是集合的键;第二个重载方法需要两个参数,分别为key与value,根据键值对删除集合中的元素

    colors.remove("green")

     

    6、Map常用操作

    6.1、遍历

    6.1.1、使用each方法

    1. Students.each {
    2. def student ->
    3. println "the key is ${student.key} and the value is ${student.value}"
    4. }

    这种方式是根据Map集合中的Entry(键值对)进行遍历的,还可以根据key与value进行遍历,需要注意的是,key与value参数的顺序不能颠倒,否则会造成数据的错乱

    1. // 直接遍历key, value而不再是遍历entry
    2. Students.each { key, value ->
    3. println "the key is ${key} and , the value is ${value}"
    4. }

    6.1.2、eachWithIndex方法

    这个是带索引的遍历

    1. // 带索引的each遍历
    2. Students.eachWithIndex { def student, int index -> // 二者的顺序不能颠倒
    3. println "the index is ${index} , the key is ${student.key} , and the value is ${student.value}"
    4. }

    这个方法同样页可以使用key与value的方式进行带索引的map集合遍历,使用eachWithIndex方法,但是同样index必须为最后一个参数,否则会造成数据错乱

    1. Students.eachWithIndex { def key, def value, int index -> // 三者的顺序不能颠倒
    2. println "the index is ${index} , the key is ${key} , and the value is ${value}"
    3. }

    6.2、Map查找操作 

    6.2.1、find方法

    这个方法借助闭包找出符合闭包条件中的第一个元素

    1. def entry = Students.find { def student ->
    2. return student.value.score >= 60
    3. }

    6.2.2、findAlll方法

    这个方法借助闭包找出符合闭包条件中的所有元素

    1. def entries = Students.findAll { def student ->
    2. return student.value.score <= 60
    3. }

    6.3、count统计方法

    这个方法借助闭包,统计符合闭包条件元素的个数,返回值为Integer

    1. // count统计方法
    2. def count = Students.count { def student ->
    3. return student.value.score >= 60 && student.value.sex == 'male'
    4. }

     

    6.4、collect方法

    对集合进行过滤,生成的是符合条件元素的列表

    1. // 查询所有学生成绩大于等于60的名字
    2. def names = Students.findAll { def student ->
    3. return student.value.score >= 60
    4. }.collect { return it.value.name } // 对集合进行过滤,生成的是符合条件学生名字的列表

    6.5、groupBy方法

    借助闭包,根据指定条件对Map集合进行分组

    1. // 根据成绩进行分组
    2. def group = Students.groupBy { def student ->
    3. return student.value.score >= 60 ? '及格' : '不及格'
    4. }

    6.6、Map排序操作

    使用sort方法,根据闭包中定义的条件对Map集合进行排序

    1. def sort = Students.sort { def student1, def student2 ->
    2. Number score1 = student1.value.score
    3. Number score2 = student2.value.score
    4. return score1 == score2 ? 0 : score1 < score2 ? -1 : 1
    5. }

    注意:list的sort排序方法是没有返回值的,而map中的sort方法是有返回值的,返回的是一个新的map集合

  • 相关阅读:
    C++ map用法总结
    js实现转义、反转义
    10个简单好用的Python装饰器
    【C#】字符串拼接相关
    python之客户端和服务端的例子
    windows 系统下 workerman 在同一个运行窗口中开启多个 websocket 服务
    PyQt5快速开发与实战 7.1 信号与槽介绍
    园子的第一款简陋鼠标垫,是否是您值得拥有的周边
    Codeforces暑期训练周报(8.22~8.28)
    电力线载波通信(PLC)简介
  • 原文地址:https://blog.csdn.net/weixin_52851967/article/details/125411740