繁体   English   中英

Java/Groovy 中的列表解析

[英]List parsing in Java/Groovy

我喜欢解析以下列表。

LUT = [
    [branch: "test", name: 'a', image_name: 'abc'],
    [branch: "test", name: 'b', image_name: 'abc'],
    [branch: "test", name: 'c', image_name: 'abc'],
    [branch: "test-1", name: 'd', image_name: 'abc'],
    [branch: "test-1", name: 'e', image_name: 'abc'],
    [branch: "test-2", name: 'f', image_name: 'abc'],
    [branch: "test-2", name: 'g', image_name: 'abc'],
    [branch: "test-2", name: 'h', image_name: 'abc'],
    [branch: "test-3", name: 'i', image_name: 'abc'],
    [branch: "test-3", name: 'j', image_name: 'abc'],
    [branch: "test-4", name: 'k', image_name: 'abc'],
    [branch: "test-5", name: 'l', image_name: 'abc'],
]

现在我要做的是:

result = [:]

    for (map in LUT)
    {
        if (!result.containsKey(map['branch']))
        {
            println map['name'] // prints the unique name
            result.put(map['branch'], map['name'])
        }
    }

这将在列表中生成 1 对 1 map:

[test:a, test-1:d, test-2:f, test-3:i, test-4:k, test-5:l]

但我想要的是一个列表列表,如:

[test:[a:abc], test-1:[d:abc], test-2:[f:abc], test-3:[i:abc], test-4:[k:abc], test-5:[l:abc]]

谁能帮我解决这个问题,谢谢

我不知道 groovy 但我想我可以提供帮助。

而不是这个:

result = [:]

for (map in LUT)
{
    if (!result.containsKey(map['branch']))
    {
        println map['name'] // prints the unique name
        result.put(map['branch'], map['name'])
    }
}

你需要这样的东西:

result = [:]

for (map in LUT)
{
    if (!result.containsKey(map['branch']))
    {
        println map['name'] // prints the unique name
        newMap = [:]
        newMap.put(map['name'], map['image_name'])
        result.put(map['branch'], newMap)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM