Convert a list of maps to one map in Groovy

I have a list of cards as shown below:

List mapOne = [[hi:1], [hello:2],[xyx:4]]

      

This card needs to be converted to one card as shown below

Map resultMap=[hi:1, hello:2,xyx:4]

      

Are there any built-in functionality in Groovy?

+3


source to share


2 answers


Just do:



Map resultMap = mapOne.collectEntries()

      

+5


source


Another option sum

:



groovy:000> [[hi:1], [hello:2], [xyx:4]].sum()
===> [hi:1, hello:2, xyx:4]

      

+1


source







All Articles