Does Groovy have method to merge 2 maps?

后端 未结 1 743
生来不讨喜
生来不讨喜 2020-12-13 16:58

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

相关标签:
1条回答
  • 2020-12-13 17:25

    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

    0 讨论(0)
提交回复
热议问题