如何获取groovy中嵌套地图的差异?
如何获取嵌套地图的差异?我不知道如何得到它。在我的地图中有 2 个元素:response1 和response2。在response1中,它的size()为5,在response2中,它的size()为3。我想打印出每个元素和缺失元素的差异
我的代码:
def map = [response1:[[code:PLACE1, date:20-Jan-2021,
[code:PLACE2, date:20-Feb-2021],
[code:PLACE3, date:20-Mar-2021],
[code:PLACE4, date:20-Apr-2021],
[code:PLACE5, date:20-May-2021]],
response2:[[code:PLACE1, date:21-Jan-2021,
[code:PLACE2, date:20-Feb-2021],
[code:PLACE3, date: 20-Mar-2021]]]
def diff1 = map['response1'].minus(map['response2'])
def diff2 = map['response2'].minus(map['response1'])
echo "${diff1}\n"
echo "${diff2}\n"
实际输出:
[[code:PLACE2, date:20-Feb-2021],
[code:PLACE4, date:20-Apr-2021],
[code:PLACE5, date:20-May-2021]]
[[code:PLACE2, date:20-Feb-2021],
[code:PLACE3, date: 20-Mar-2021]]
预期输出:
[[date:20-Jan-2021],
[code:PLACE4, date:20-Apr-2021],
[code:PLACE5, date:20-May-2021]]
[[date:21-Jan-2021],
How to get the difference of nested maps? I cant figure out how to get it. In my map there are 2 elements: response1 and response2. In response1 it has 5 in size() and in response2 it has 3 in size(). I want to print out the difference in each element and the missing elements as well
My Code:
def map = [response1:[[code:PLACE1, date:20-Jan-2021,
[code:PLACE2, date:20-Feb-2021],
[code:PLACE3, date:20-Mar-2021],
[code:PLACE4, date:20-Apr-2021],
[code:PLACE5, date:20-May-2021]],
response2:[[code:PLACE1, date:21-Jan-2021,
[code:PLACE2, date:20-Feb-2021],
[code:PLACE3, date: 20-Mar-2021]]]
def diff1 = map['response1'].minus(map['response2'])
def diff2 = map['response2'].minus(map['response1'])
echo "${diff1}\n"
echo "${diff2}\n"
Actual Output:
[[code:PLACE2, date:20-Feb-2021],
[code:PLACE4, date:20-Apr-2021],
[code:PLACE5, date:20-May-2021]]
[[code:PLACE2, date:20-Feb-2021],
[code:PLACE3, date: 20-Mar-2021]]
Expected Output:
[[date:20-Jan-2021],
[code:PLACE4, date:20-Apr-2021],
[code:PLACE5, date:20-May-2021]]
[[date:21-Jan-2021],
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您所追求的是某种可用于嵌套地图和列表的
minus
函数。这是一种使用递归产生预期结果的方法。I believe what you are after is some kind of
minus
function that can be used for nested Maps and Lists. Here is a method that produces the expected result using recursion.