Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Groovy have method to merge 2 maps?

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

like image 757
fedor.belov Avatar asked Nov 10 '12 23:11

fedor.belov


People also ask

Are Groovy maps ordered?

2. The each Method. In Groovy, maps created with the literal notation are ordered.

How do I concatenate a map in Java?

concat() Alternatively, we can use Stream#concat() function to merge the maps together. This function can combine two different streams into one. As shown in the snippet, we are passed the streams of map1 and map2 to the concate() function and then collected the stream of their combined entry elements.


1 Answers

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

like image 124
tim_yates Avatar answered Sep 27 '22 20:09

tim_yates