如何获取groovy中嵌套地图的差异?

发布于 2025-01-10 01:15:38 字数 964 浏览 0 评论 0原文

如何获取嵌套地图的差异?我不知道如何得到它。在我的地图中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2025-01-17 01:15:38

我相信您所追求的是某种可用于嵌套地图和列表的 minus 函数。这是一种使用递归产生预期结果的方法。

def minus(left, right) {
    if (left == right) {
        assert left in Map || left in List
        return left.getClass().getDeclaredConstructor().newInstance()
    } else {
        switch (left) {
            case Map:
                if (!(right instanceof Map)) {
                    return left
                } else {
                    return left.findAll { k, v -> !right.containsKey(k) } \
                           + left.findAll { k, v -> right.containsKey(k) && v != right[k] }
                                    .collectEntries { k, v -> [k, minus(v, right[k])] }
                }
            case List:
                if (!(right instanceof List)) {
                    return left
                } else {
                    return left.withIndex().with {
                        it.findAll { val, index -> index < right.size() && val != right[index] }
                                .collect { val, index -> minus(val, right[index]) } \
                        + it.findAll { val, index -> index >= right.size() }
                                .collect { val, index -> val }
                    }
                }
            default:
                return left
        }
    }
}

def 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"]]

def response2 = [[code: "PLACE1", date: "21-Jan-2021"],
                 [code: "PLACE2", date: "20-Feb-2021"],
                 [code: "PLACE3", date: "20-Mar-2021"]]

assert minus(response1, response2) == [['date': '20-Jan-2021'], ['code': 'PLACE4', 'date': '20-Apr-2021'], ['code': 'PLACE5', 'date': '20-May-2021']]
assert minus(response2, response1) == [['date': '21-Jan-2021']]

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.

def minus(left, right) {
    if (left == right) {
        assert left in Map || left in List
        return left.getClass().getDeclaredConstructor().newInstance()
    } else {
        switch (left) {
            case Map:
                if (!(right instanceof Map)) {
                    return left
                } else {
                    return left.findAll { k, v -> !right.containsKey(k) } \
                           + left.findAll { k, v -> right.containsKey(k) && v != right[k] }
                                    .collectEntries { k, v -> [k, minus(v, right[k])] }
                }
            case List:
                if (!(right instanceof List)) {
                    return left
                } else {
                    return left.withIndex().with {
                        it.findAll { val, index -> index < right.size() && val != right[index] }
                                .collect { val, index -> minus(val, right[index]) } \
                        + it.findAll { val, index -> index >= right.size() }
                                .collect { val, index -> val }
                    }
                }
            default:
                return left
        }
    }
}

def 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"]]

def response2 = [[code: "PLACE1", date: "21-Jan-2021"],
                 [code: "PLACE2", date: "20-Feb-2021"],
                 [code: "PLACE3", date: "20-Mar-2021"]]

assert minus(response1, response2) == [['date': '20-Jan-2021'], ['code': 'PLACE4', 'date': '20-Apr-2021'], ['code': 'PLACE5', 'date': '20-May-2021']]
assert minus(response2, response1) == [['date': '21-Jan-2021']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文