我是靠谱客的博主 明理小虾米,最近开发中收集的这篇文章主要介绍Gradle自动化构建(四) Groovy list、map、range,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

###list
//def list = new ArrayList() // java
def list = [1, 2, 3, 4, 5] // 定义list
println list.class // class java.util.ArrayList
println list.size() // 5

def arr1 = [1, 2, 3] as int[] // 使用 as 关键字将list转换为数组
int[] arr2 = [1, 3, 4] // 定义数组

def sortList = [6, -3, 9, 2, -7, 1, 5]
Comparator mc = { // 自定义排序规则,按绝对值大小排序
    a, b -> a==b ? 0 : (Math.abs(a)<Math.abs(b) ? -1 : 1)
}
Collections.sort(sortList, mc)
println sortList // [1, 2, -3, 5, 6, -7, 9]

def sort = [6, -3, 9, 2, -7, 1, 5]
sort.sort { // sort的闭包可以有很多种形式,参看源码
//    a, b -> a==b ? 0 : (Math.abs(a)<Math.abs(b) ? -1 : 1) // 按绝对值大小排序, [1, 2, -3, 5, 6, -7, 9]
    it -> return it // 按其本身值大小排序, [-7, -3, 1, 2, 5, 6, 9]
}
println sort

def fs = [6, -3, 9, 2, -7, 1, 5]
println fs.find { it%2==0 } // 6
println fs.findAll { it%2==0 } // [6, 2]
println fs.any { it%2==0 } // true
println fs.every { it%2==0 } // false
println fs.min() { Math.abs(it) } // 1 , 返回绝对值最小的元素
println fs.max() // 9 , 也可以使用闭包
println fs.count { it%2==0 } // 2 , 统计,符合闭包中的规则的元素的个数

###map
def colors = [red: ‘ff0000’, ‘green’: ‘00ff00’, blue: ‘0000ff’] as Hashtable // 定义map,key值建议使用单引号的字符串,如果key值没加引号,编译其会自动添加
println colors.getClass() // class java.util.Hashtable , 默认为 java.util.LinkedHashMap , 使用 colors.class 表示获取map中key为class的value值
println colors[‘red’] // ff0000 , 获取map中key为red的值
println colors.red // ff0000 , 获取map中key为red的值
colors.yellow = ‘ffff00’ // 当map中的key没有yellow时,添加,有yellow时,修改对应的value值
colors.complex = [a:1, b:2] // 给map添加map, map中可以添加任意类型的对象
println colors // [red:ff0000, green:00ff00, blue:0000ff, yellow:ffff00, complex:[a:1, b:2]]

def students = [
        1: [number: '0001', name: 'Bob',
            score : 55, sex: 'male'],
        2: [number: '0002', name: 'Johnny',
            score : 62, sex: 'female'],
        3: [number: '0003', name: 'Claire',
            score : 73, sex: 'female'],
        4: [number: '0004', name: 'Amy',
            score : 66, sex: 'male']
]
students.each { // 遍历
//    key is 1, value is [number:0001, name:Bob, score:55, sex:male]
//    key is 2, value is [number:0002, name:Johnny, score:62, sex:female]
//    key is 3, value is [number:0003, name:Claire, score:73, sex:female]
//    key is 4, value is [number:0004, name:Amy, score:66, sex:male]
    def student -> println "key is $student.key," + " value is $student.value"
}
students.eachWithIndex { // 遍历,带下标
//    index is 0, key is 1, value is [number:0001, name:Bob, score:55, sex:male]
//    index is 1, key is 2, value is [number:0002, name:Johnny, score:62, sex:female]
//    index is 2, key is 3, value is [number:0003, name:Claire, score:73, sex:female]
//    index is 3, key is 4, value is [number:0004, name:Amy, score:66, sex:male]
    def student, int index -> println "index is $index," + " key is $student.key," + " value is $student.value"
}
students.each { // 遍历,闭包参数直接传 key value , 也有 eachWithIndex 带下标
//    key is 1, value is [number:0001, name:Bob, score:55, sex:male]
//    key is 2, value is [number:0002, name:Johnny, score:62, sex:female]
//    key is 3, value is [number:0003, name:Claire, score:73, sex:female]
//    key is 4, value is [number:0004, name:Amy, score:66, sex:male]
    key, value -> println "key is $key," + " value is $value"
}
println students.find { it.value.score >= 60 } // 2={number=0002, name=Johnny, score=62, sex=female} , 查找第一个score大于等于60的元素
println students.findAll { it.value.score >= 66 } // [3:[number:0003, name:Claire, score:73, sex:female], 4:[number:0004, name:Amy, score:66, sex:male]]
println students.count { it.value.score>=60 && it.value.sex=='male' } // 1
println students.findAll { it.value.score >= 66 }.collect { it.value.name } // [Claire, Amy] , 查找score大于等于66的学生的name
def group = students.groupBy { it.value.score>=60 ? "及格" : "不及格" } // 分组
// [不及格:[1:[number:0001, name:Bob, score:55, sex:male]], 及格:[2:[number:0002, name:Johnny, score:62, sex:female], 3:[number:0003, name:Claire, score:73, sex:female], 4:[number:0004, name:Amy, score:66, sex:male]]]
println group.toMapString()

def sort = students.sort { // 排序,与list不一样的地方是,map的sort返回的是新的排好序的map,是不改变students值的,而list是改变原list的值,并不返回list
    def student1, def student2 ->
    Number score1 = student1.value.score // Number 可以表示整型,也可以表示浮点型
    Number score2 = student2.value.score
    return score1 == score2 ? 0 : score1 < score2 ? -1 : 1
}
// [1:[number:0001, name:Bob, score:55, sex:male], 2:[number:0002, name:Johnny, score:62, sex:female], 4:[number:0004, name:Amy, score:66, sex:male], 3:[number:0003, name:Claire, score:73, sex:female]]
println sort.toMapString()

###range
def range = 1…10 // range本质就是一个list类
println range[1] // 2 , 获取下标1的值
println range.contains(10) // true , range中是否包含10
println range.from // 1 , 其实值
println range.to // 10 , 终点值

range.each { // 遍历
    println it
}

def getGrade(Number number) {
    def ret
    switch (number) {
        case 0..<60: ret = '不及格'; break // <60 表示range不取60
        case 60..<70: ret = '及格'; break
        case 70..<80: ret = '良好'; break
        case 80..100: ret = '优秀'; break
    }
    return ret
}
println getGrade(75) // 良好

最后

以上就是明理小虾米为你收集整理的Gradle自动化构建(四) Groovy list、map、range的全部内容,希望文章能够帮你解决Gradle自动化构建(四) Groovy list、map、range所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部