Contents

Groovy 中Map迭代

1. 简介

在这个简短的教程中,我们将研究在 Groovy 中使用标准语言特性(例如eacheachWithIndex和 for-in循环)迭代Map的方法 。

2. each方法

假设我们有以下Map:

def map = [
    'FF0000' : 'Red',
    '00FF00' : 'Lime',
    '0000FF' : 'Blue',
    'FFFF00' : 'Yellow'
]

我们可以通过为each 方法提供一个简单的闭包来迭代Map:

map.each { println "Hex Code: $it.key = Color Name: $it.value" }

我们还可以通过为入口变量命名来提高可读性:

map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" }

或者,如果我们更愿意分别处理键和值,我们可以在闭包中分别列出它们:

map.each { key, val ->
    println "Hex Code: $key = Color Name $val"
}

在 Groovy 中,使用文字符号创建的映射是有序的。 我们可以期望我们的输出与我们在原始Map中定义的顺序相同。

3. eachWithIndex方法

有时我们想在迭代时知道index

例如,假设我们想要在Map中每隔一行缩进一次。为了在 Groovy 中做到这一点,我们将使用 eachWithIndex 方法和entryindex变量:

map.eachWithIndex { entry, index ->
    def indent = ((index == 0 || index % 2 == 0) ? "   " : "")
    println "$index Hex Code: $entry.key = Color Name: $entry.value"
}

与 each方法一样,我们可以选择在闭包中使用 keyvalue变量而不是entry

map.eachWithIndex { key, val, index ->
    def indent = ((index == 0 || index % 2 == 0) ? "   " : "")
    println "$index Hex Code: $key = Color Name: $val"
}

4. 使用For-in循环

另一方面,如果我们的用例更适合命令式编程,我们也可以使用for-in语句来迭代我们的Map:

for (entry in map) {
    println "Hex Code: $entry.key = Color Name: $entry.value"
}