如何使用嵌套的subMap查找subMap列表

时间:2016-09-14 05:37:21

标签: groovy

我有ListMap嵌套Map以及以下内容: -

def list = [
            [
                "description": "The issue is open and ready for the assignee to start work on it.",
                "id": "1",
                "name": "Open",
                "statusCategory": [
                    "colorName": "blue-gray",
                    "id": 2,
                    "key": "new",
                    "name": "To Do",
                ]
            ],
            [
                "description": "This issue is being actively worked on at the moment by the assignee.",
                "id": "3",
                "name": "In Progress",
                "statusCategory": [
                    "colorName": "yellow",
                    "id": 4,
                    "key": "indeterminate",
                    "name": "In Progress",
                ]
            ]
        ]

我有一项任务是List subMap嵌套subMap。我做的事情如下: -

def getSubMap = { lst ->
            lst.findResults { it.subMap(["id", "name", "statusCategory"])}
}

println getSubMap(list)

但它给我输出如下: -

[
  [
    "id":1, 
    "name":"Open", 
    "statusCategory":[
        "colorName":"blue-gray", 
        "id":2, 
        "key":"new", 
        "name":"To Do"
    ]
  ], 
  [
    "id":"3", 
    "name":"In Progress", 
    "statusCategory":[
       "colorName":"yellow", 
       "id":"4", 
       "key":"indeterminate", 
       "name":"In Progress"
   ]
  ]
]

正如您所见,我无法获得subMap statusCategory个密钥Map subMap。实际上我希望进一步Maps嵌套[ [ "id":1, "name":"Open", "statusCategory":[ "id":"2", "name":"To Do" ] ], [ "id":"3", "name":"In Progress", "statusCategory":[ "id":"4", "name":"In Progress" ] ] ] ,如下所示: -

def getSubMap = { lst ->
            lst.findResults { it.subMap(["id", "name", "statusCategory":["id","name"]])}
}

def modifiedList = getSubMap(list)

为实现这一点,我尝试如下: -

Excpetion

但它让我def getSubMap = { lst -> lst.findResults { it.subMap(["id", "name", "statusCategory"]).statusCategory.subMap(["id","name"])} } println getSubMap(list) 。如果我按照以下方式进行操作: -

subMap

它只提供嵌套的 [["id":"2", "name":"To Do"], ["id":"4", "name":"In Progress"]] : -

List

如果存在,有人可以建议我如何使用嵌套subMap再次找到subMap的{​​{1}}吗?

2 个答案:

答案 0 :(得分:1)

鉴于原始list,请考虑以下事项:

def resultList = list.collect { 
    def fields = ["id", "name"]
    def m = it.subMap(fields)
    m["statusCategory"] = it["statusCategory"].subMap(fields)
    return m
}

支持这些断言:

assert 1 == resultList[0]["id"] as int
assert "Open" == resultList[0]["name"]
assert 2 == resultList[0]["statusCategory"]["id"] as int
assert "To Do" == resultList[0]["statusCategory"]["name"]

assert 3 == resultList[1]["id"] as int
assert "In Progress" == resultList[1]["name"]
assert 4 == resultList[1]["statusCategory"]["id"] as int
assert "In Progress" == resultList[1]["statusCategory"]["name"]

答案 1 :(得分:1)

如果您的Map嵌套是任意的,那么您可能需要考虑这样的事情:

def nestedSubMap
nestedSubMap = { Map map, List keys ->
    map.subMap(keys) + map.findAll { k, v -> v instanceof Map }.collectEntries { k, v -> [(k):nestedSubMap(v, keys)] }
}

根据您的输入和此闭包,以下脚本:

def result = list.collect { nestedSubMap(it, ["id", "name"]) }
println '['
result.each { print it; println ',' }
println ']'

生成此输出:

[
[id:1, name:Open, statusCategory:[id:2, name:To Do]],
[id:3, name:In Progress, statusCategory:[id:4, name:In Progress]],
]