I have a list of map like below:
List mapOne = [[hi:1], [hello:2],[xyx:4]]
This map should be converted to one single map like below
Map resultMap=[hi:1, hello:2,xyx:4]
Do we have any built in functions in Groovy?
Using a SpreadMap we will convert a list of strings into a map. A SpreadMap is a helper that turns a list with an even number of elements into a Map. In the snippet below, we create a map keyed by NFL city while the value will be the team name.
2. Creating Groovy Maps. We can use the map literal syntax [k:v] for creating maps. Basically, it allows us to instantiate a map and define entries in one line.
A hashmap maps a key to a value, but you are trying to map a key to two values. To do this, create a hashmap, where the key is your ID and the value is another hashmap, mapping the string "firstname" to "Jack" and the string "lastname" to "Sparrow" and so on for all <person> elements. Morten.
Just do:
Map resultMap = mapOne.collectEntries()
Another option is sum
:
groovy:000> [[hi:1], [hello:2], [xyx:4]].sum()
===> [hi:1, hello:2, xyx:4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With