Does Groovy have method to merge 2 maps?

DictionaryGroovy

Dictionary Problem Overview


First map is default options [a: true, b: false]. Second map - options passed by user [a:false]. Does Groovy has maps merge method to obtain [a: false, b:false]?

It's not problem to implement it in Groovy. I'm asking about method out of the box

Dictionary Solutions


Solution 1 - Dictionary

You can use plus:

assert [ a: true, b: false ] + [ a: false ] == [ a: false, b: false ]

Or left shift:

assert [ a: true, b: false ] << [ a: false ] == [ a: false, b: false ] 

The difference is that << adds the right hand map into the left hand map. When you use +, it constructs a new Map based on the LHS, and adds the right hand map into it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionfedor.belovView Question on Stackoverflow
Solution 1 - Dictionarytim_yatesView Answer on Stackoverflow