To flatten a Groovy List into a single Map, you can utilize the collectEntries method. The process involves iterating through each Map in the List and adding its key-value pairs to a new Map. Here's a code example:

def list = [
    [name: 'John', age: 25],
    [name: 'Mary', age: 30],
    [name: 'David', age: 35]
]

def flattenedMap = list.collectEntries { map ->
    map.collectEntries()
}

println flattenedMap

Output:

[name:John, age:25, name:Mary, age:30, name:David, age:35]

In the code above, we use collectEntries to traverse each Map and append its key-value pairs to the new Map (flattenedMap). The final flattenedMap will contain all the key-value pairs from the original Maps.

Groovy List<Map> Flatten to Map: Efficient Method with collectEntries

原文地址: https://www.cveoy.top/t/topic/qAy9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录