Groovy List<Map> Flatten to Map: Efficient Method with collectEntries
To flatten a Groovy List
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.
原文地址: https://www.cveoy.top/t/topic/qAy9 著作权归作者所有。请勿转载和采集!