Groovy List<Map> Flatten into a Single Map: Guide and Example
To flatten a Groovy List<Map> into a single Map, you can utilize the flatten() method and the inject() method of collections. Here's an example code demonstrating this:
def list = [
[name: 'John', age: 25],
[name: 'Jane', age: 30],
[name: 'Bob', age: 35]
]
def result = list.flatten().inject([:]) { map, item ->
map.putAll(item)
map
}
println result
The output will be:
[name:John, age:25, name:Jane, age:30, name:Bob, age:35]
This demonstrates how the original List<Map> has been flattened into a single Map.
原文地址: https://www.cveoy.top/t/topic/qAGl 著作权归作者所有。请勿转载和采集!