Groovy 数据结构

Groovy数据结构

本文链接:https://blog.csdn.net/feather_wch/article/details/131623361

List

/***============================================
 * List相关操作S
 *
 *=========================================*/
def list=[1,2,3,4,5] // 默认ArrayList
println(list.toListString())
println(list.class)

def array=[1,2,3,4,5] as int[] //int数组
println(array.class)

// 1、添加
list.add(10)
list << 7
def addList = list + 5
println(addList)

list.add(3, 9) // index = 3, add 9

// 2、删除
println(list)
list.remove(5) // 删除index=4的数据
list.remove((Object)5) // 删除数据4
list.removeElement(5) // 删除数据4,自动转换
list.removeAll{
    return it % 2 != 0 // 删除奇数,闭包Closure
}
list = list - [2, 4] // 删除集合
println(list)

// 3、查找
// 找到符合条件的第一个
def findList = [2, 3, 4, 7, 10]
int result = findList.find{
    return it % 2 == 0 // 找到第一个偶数
}
println(result)
// 找到所有满足条件的数据集合
def result2 = findList.findAll{
    return it % 2 == 0 // 找到第一个偶数
}
println(result2)

// true or false: 找到一个满足条件的
def result3 = findList.any{
    return it % 2 == 0 // 找到第一个偶数
}
// true or false: 需要每个都满足条件
def result4 = findList.every{
    return it % 2 == 0 // 找到第一个偶数
}

// 最小值
def result5 = findList.min{
    return Math.abs(it)
}
println(result5)
// 最大值
def result6 = findList.max{
    return Math.abs(it)
}
println(result6)

// 统计
def result7 = findList.count{
    return it > 3
}
println(result7)

// 4、排序
def sortList = [5, -2, 1, 3, 4]
sortList.sort() // 从小到大默认排序
// 绝对值,从小到大排序
sortList.sort({a, b ->
    a == b ? 0 : Math.abs(a)>Math.abs(b) ? 1 : -1
})

// 按照字母个数排序
def sortStringList = ['abds', 'dxsawfs', 'a', 'vd', 'xxsadsawdasdawd']
sortStringList.sort{
    it.size()
}

println(sortStringList)

Map

/***============================================
 * Map相关操作
 *
 *=========================================*/
// 默认LinkedHashMap
def map = [red:'ff0000', green:'00ff00', blue:'0000ff']
// 'ff0000'是String
println(map['red'])
println(map.red)
// getClass拿到map的class
println(map.getClass())

// 可以创建其他类型HashMap
def newMap = [red:'ff0000', green:'00ff00'] as ConcurrentHashMap
println(newMap)

// 1、添加数据
// 添加普通对象
map.yellow='ffff00'
println(map)
// 添加集合对象
map.map = [key1:1, key2:2]
println(map)

//遍历map
def teachers = [
        1: [number: '001', name: 'jett'],
        4: [number: '004', name: 'alven'],
        3: [number: '003', name: 'lance'],
        2: [number: '002', name: 'leo'],
        6: [number: '006', name: 'allen'],
        5: [number: '005', name: 'zero'],
        7: [number: '007', name: 'derry'],
        8: [number: '008', name: 'jett']
]
// 键值对
teachers.each{def key, def value ->
    println("key = ${key} value = ${value}")
}
// Entry对象
teachers.each{def teacher ->
    println("key = ${teacher.key} value = ${teacher.value}")
}
// 带索引 + Entry
teachers.eachWithIndex{def teacher, int index ->
    println("带索引 + Entry")
    println("index = ${index} key = ${teacher.key} value = ${teacher.value}")
}
// 带索引 + key,value
teachers.eachWithIndex{def key, def value, int index ->
    println("带索引 + key,value")
    println("index = ${index} key = ${key} value = ${value}")
}


// 2、查找数据
def entry = teachers.find{def teacher ->
    return teacher.value.name == 'derry'
}
println entry

def entryAll = teachers.findAll{def teacher ->
    return teacher.value.name == 'derry'
}
println entryAll

def count = teachers.count{def teacher ->
    return teacher.value.name == 'derry'
}
println count

// 实现嵌套查询
// 1. 找到所有符合name = jett的集合
def number = teachers.findAll {
    def teacher -> teacher.value.name == 'jett'
}.collect {
    // 2. 组合其number为集合,并且返回
    it.value.number
}
println(number)
// 分组查询
def group = teachers.groupBy {
    it.value.name == 'jett' ? 'groupa' : 'groupb'
}
println(group)

// 排序Map会返回新的map。List只是在原来list中操作
def sort = teachers.sort{
    def t1, def t2 -> t1.key > t2.key ? 1 : -1
}
println(sort.toMapString())

Range

//定义
//Range相当于一个轻量级的List
def range=1..10
println range[0]
println range.contains(8)
println range.from //起点 1
println range.to   //终点 10

遍历
range.each {
    println it
}
for(i in range){
    println i
}

def getGrade(Number score){
    def result
    switch(score){
        case 0:
            result = '滚蛋吧'
            break
        case 0<..<60:
            result='不及格'
            break;
        case 60..100:
            result='及格'
            break;
        default:
            result='输入异常'
    }
    return result
}
println getGrade(50)
println getGrade(80)
println getGrade(120)
println getGrade(0)

你可能感兴趣的:(Groovy,gradle,数据结构,开发语言)