Groovy 有合并 2 个地图的方法吗? [英] Does Groovy have method to merge 2 maps?
本文介绍了Groovy 有合并 2 个地图的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
第一张地图是默认选项[a: true, b: false]
.第二张地图 - 用户 [a:false]
传递的选项.Groovy 是否有映射合并方法来获取 [a: false, b:false]
?
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]
?
在 Groovy 中实现它不是问题.我正在询问开箱即用的方法
It's not problem to implement it in Groovy. I'm asking about method out of the box
推荐答案
您可以使用加号:
assert [ a: true, b: false ] + [ a: false ] == [ a: false, b: false ]
或左移:
assert [ a: true, b: false ] << [ a: false ] == [ a: false, b: false ]
区别在于<<
将右手地图添加到左手地图.当您使用 +
时,它根据LHS构造一个新的Map,并在其中添加右手图
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
这篇关于Groovy 有合并 2 个地图的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文