Groovy中的列表映射范围数据类型的相关操作

2024-01-02 18:16:21

1 列表
1.1 列表的定义

? 可以通过下面两种方式定义列表:一直是方式和在java中一样,一种是groovy的语法自带的。

package com.dream21th

//定义集合方式一,参照java的方式定义
def list=new ArrayList()

//定义集合的方式二,采用groovy的方式定义
def list2=[1, 2, 3, 4, 5]
println(list2.class)//class java.util.ArrayList
println(list2.size())//5

//定义数组
def arr=[1,2,3,4] as int[]
int[] arr2=[1,2,3,4]
println(arr.class)//class [I
println(arr2.class)//class [I
1.2 添加和删除元素

? 向列表中添加元素有下面几种方式:

//列表增加元素操作
def list= [1,2,3,4,5,6,7]

//列表添加元素的方式一
list.add(8)
println(list) //[1, 2, 3, 4, 5, 6, 7, 8]

//列表添加元素的方式二
list.leftShift(9)
println(list)

//列表添加元素的方式三
list << 10
println(list)

//列表添加元素的方式四(这里会生成一个新的列表)
def newList = list + 11
println(newList)

? 从列表中删除元素的方式有:

//列表删除元素操作
def list2= [1,2,3,4,5,6,7]

//根据索引删除元素(例子是删除索引为4的元素)
list2.remove(4)
println(list2)//[1, 2, 3, 4, 6, 7]

//按照指定元素删除,由于此数组是数字类型,所以需要转换不然会按照索引删除
list2.remove((Object)3)
println(list2)//[1, 2, 4, 6, 7]

//删除元素值为2的值
list2.removeElement(2)
println(list2)//[1, 4, 6, 7]

//删除所有偶数的值
def list3= [1,2,3,4,5,6,7]
list3.removeAll(it->it%2==0)
println(list3)//[1, 3, 5, 7]

//使用操作符删除元素
def list4= [1,2,3,4,5,6,7]
def newList4= list4 - [5,7,8]
println(newList4)//[1, 2, 3, 4, 6]
1.3 列表中元素排序

? 列表元素的排序有下面几种:

package com.dream21th

def list= [3,7,5,-2,9,0]

//升序操作
list.sort()
println(list) //[-2, 0, 3, 5, 7, 9]

//数组排序反转
list.reverse(true)
println(list)//[9, 7, 5, 3, 0, -2]

//按照绝对值大小排序,闭包
def list2= [3,7,5,-2,9,0]
list2.sort{num1,num2 -> Math.abs(num1)>=Math.abs(num2)?1:-1}
println(list2)//[0, -2, 3, 5, 7, 9]

//按照绝对值大小排序
def list3= [3,7,5,-2,9,0]
list3.sort(Comparator.comparing(it->Math.abs(it)))
println(list3)//[0, -2, 3, 5, 7, 9]
1.4 列表中查找元素

? 列表中查找元素有下面的方法:

package com.dream21th

def list= [3,7,5,-2,9,0]

//查找列表中第一个偶数
println(list.find {it%2==0})//-2
//查找列表中所有偶数
println(list.findAll {it%2==0})//[-2, 0]

//列表中只要有一个元素是偶数就返回true
println(list.any {it%2==0})//true

//列表中每一个元素都是偶数就返回true
println(list.every {it%2==0})//false

//获取列表中最大元素
println(list.max())//9
//获取列表中最小元素
println(list.min())//-2

//统计偶数的个数
println(list.count {it%2==0})//2
2 映射
2.1 映射的定义

? 可以参照下面方式定义映射,并做一些操作:

package com.dream21th

def map1=[
        "name":"张三",
        "age": 18,
        "phone":"13777771234"
]

println(map1) //[name:张三, age:18, phone:13777771234]

map1.'address' = "上海市浦东新区"
println(map1)//[name:张三, age:18, phone:13777771234, address:上海市浦东新区]

map1["hobby"]=["跑步","游泳"]
println(map1)//[name:张三, age:18, phone:13777771234, address:上海市浦东新区, hobby:[跑步, 游泳]]

map1.'scores'=[
        "语文":99,
        "数学":100
]
println(map1)//[name:张三, age:18, phone:13777771234, address:上海市浦东新区, hobby:[跑步, 游泳], scores:[语文:99, 数学:100]]

//定义map默认采用的是java.util.LinkedHashMap
println(map1.getClass())//class java.util.LinkedHashMap

//可以通过下面方法指定map
def map2=[
        "name":"张三",
        "age": 18,
        "phone":"13777771234"
] as Hashtable
println(map2.getClass())//class java.util.Hashtable

2.2 映射的遍历

? 可以按照下面方式遍历映射:

package com.dream21th


def map1=[
        "name":"张三",
        "age": 18,
        "phone":"13777771234"
]

//遍历方式一
map1.each {it -> println(it.key+"   "+it.value)}
//name   张三
//age   18
//phone   13777771234

//遍历方式二
map1.each {key,value -> println(key+"   "+value)}
//name   张三
//age   18
//phone   13777771234

//带索引遍历
map1.eachWithIndex{ Map.Entry<String, Serializable> entry, int i -> printf("index:%d,key:%s,value:%s\n",i,entry.key,entry.value)}
//index:0,key:name,value:张三
//index:1,key:age,value:18
//index:2,key:phone,value:13777771234


map1.eachWithIndex{ key,value, int i ->printf("index:%d,key:%s,value:%s\n",i,key,value) }
//index:0,key:name,value:张三
//index:1,key:age,value:18
//index:2,key:phone,value:13777771234

2.3 映射的查找和排序

? 可以按照下面的代码对映射中的值进行查找和排序:

package com.dream21th

def map1=[
        "stu1":new People(name: "张三",age:18,phone: "139711111234"),
        "stu2":new People(name: "里斯",age:19,phone: "141711111234"),
        "stu3":new People(name: "王五",age:16,phone: "151711111234"),
        "stu4":new People(name: "赵六",age:20,phone: "134711111234"),
]

class People{
    //姓名
    String name
    //年龄
    int age
    //电话
    String phone

    @Override
    String toString() {
        return "{name:"+name+",age:"+age+",phone:"+phone+"}"
    }
}

//找到map中第一个年龄等于18的
println(map1.find{it -> it.value.age==18})//stu1={name:张三,age:18,phone:139711111234}

//找到map中年龄大于等于18的所有值
println(map1.findAll{it -> it.value.age>=18})//[stu1:{name:张三,age:18,phone:139711111234}, stu2:{name:里斯,age:19,phone:141711111234}, stu4:{name:赵六,age:20,phone:134711111234}]

//找到map中年龄大于等于18的所有值的key
println(map1.findAll{it -> it.value.age>=18}.collect {it -> it.key}) //[stu1, stu2, stu4]

//分组
println(map1.groupBy{it -> it.value.age>=18?"成年":"未成年"})//[成年:[stu1:{name:张三,age:18,phone:139711111234}, stu2:{name:里斯,age:19,phone:141711111234}, stu4:{name:赵六,age:20,phone:134711111234}], 未成年:[stu3:{name:王五,age:16,phone:151711111234}]]

//排序
def map2 = map1.sort {stu1,stu2 -> stu1.value.age >= stu2.value.age ?1:-1}
println(map2)//[stu3:{name:王五,age:16,phone:151711111234}, stu1:{name:张三,age:18,phone:139711111234}, stu2:{name:里斯,age:19,phone:141711111234}, stu4:{name:赵六,age:20,phone:134711111234}]
3 范围

? 范围是在groovy中的定义方式和使用如下:

package com.dream21th

//2到9的闭区间
def r=2..9
println(r)//2..9
println(r.size())//8

//9不包含
def r1=2..<9
println(r1)//2..<9
println(r1.size())//7

//索引为0的元素
println(r1[0])//2
//范围是否包含5
println(r1.contains(5))//true
//范围的起始值
println(r1.from)//2
//范围的终止值
println(r1.to)//8

//遍历打印
r1.each {it -> println(it)}

//遍历打印
for(e in r1){
    println(e)
}

文章来源:https://blog.csdn.net/qq_36305027/article/details/135337522
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。