Gradle系列四之集合

作者: zhang_pan | 来源:发表于2019-05-27 15:02 被阅读2次
List

Java中是如何创建的,举个例子:

def list = new ArrayList()

而在groovy中,还可以这样定义:

def list = [1, 2, 3, 4, 5]

1. add方法

list.add(5)

打印输出如下:

[1, 2, 3, 4, 5, 5]

add方法还可以用<<箭头替代:

list << 1

打印输出如下:

[1, 2, 3, 4, 5, 1]

2. remove方法

list.remove(1)

打印输出如下:

[1, 3, 4, 5]

删除的是坐标为1的元素2

3. removeElement()方法

list.removeElement(1)

打印输出如下:

[2, 3, 4, 5]

4. removeAll()方法

list.removeAll {
    it % 2 != 0
}

打印输出如下:

[2, 4]

5. 减号操作符

println list - [3]

打印输出如下:

[1, 2, 4, 5]

6. 排序

def sortList = [4, -5, 9, -8, 7, -3, -2, 1, -6, -4]
sortList.sort {
    a, b -> Math.abs(a) >= Math.abs(b) ? 1 : -1
}
println sortList

打印输出如下:

[1, -2, -3, 4, -4, -5, -6, 7, -8, 9]

7. 查找
find函数:

def findList = [4, -5, 9, -8, 7, -3, -2, 1, -6, -4]
println findList.find {
    it % 2 != 0
}

打印输出如下:

-5

findAll函数:

println findList.findAll {
    it % 2 == 0
}

打印输出如下:

[4, -8, -2, -6, -4]

any函数:

println findList.any {
    it % 2 == 0
}

打印输出如下:

true

every函数:

println findList.every {
    it % 2 == 0
}

打印输出如下:

false

min函数:

println findList.min {
    Math.abs(it)
}

打印输出如下:

1

count函数:

println findList.count {
    it % 2 == 0
}

打印输出如下:

5
Map

在Groovy中,可以这样定义映射:

def colors = [red: 'ff0000',
              green: '00ff00',
              blue: '0000ff']

如何获取red这个key对应的value呢?

println colors['red']
println colors.red

这两种方法都是可以的
向map中添加一个元素:

colors.black = 'fff'

输出这个colors为:

[red:ff0000, green:00ff00, blue:0000ff, black:fff]

还可以更复杂,比如添加的元素又是一个映射:

colors.complex = [a: 1, b: 2]
println colors

打印输出如下:

[red:ff0000, green:00ff00, blue:0000ff, complex:[a:1, b:2]]

1. 遍历
each函数:

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 {student ->
    println "key is ${student.key}, value is ${student.value}"
}

eachWithIndex函数:

students.eachWithIndex{ student, index ->
    println "index is ${index}, key is ${student.key}, value is ${student.value}"
}

each函数,直接遍历key-value:

students.each {key, value ->
    println "key is ${key}, value is ${value}"
}

eachWithIndex函数,直接遍历key-value:

students.eachWithIndex{key, value, index ->
    println "index is ${index}, key is ${key}, value is ${value}"
}

2. 查找
find、findAll等函数在这里就不再介绍了,这里主要介绍下groupBy:

println students.groupBy {student ->
    student.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]]]

3. 排序

println students.sort {student1, student2 ->
    Number score1 = student1.value.score as Number
    Number score2 = student2.value.score as Number
    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]]

喜欢本篇博客的简友们,就请来一波点赞,您的每一次关注,将成为我前进的动力,谢谢!

相关文章

网友评论

    本文标题:Gradle系列四之集合

    本文链接:https://www.haomeiwen.com/subject/qekhtctx.html