Groovy有没有办法合并2张地图? [英] Does Groovy have method to merge 2 maps?

查看:94
本文介绍了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 构建一个新的地图,并添加右手图。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆