0

I like to parse the following list.

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'],
]

For now what i do is :

result = [:]

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

This will result in a 1 to 1 map in a list:

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

But what i want to have is a list of lists like:

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

Can any one assist me on this , Thank you

1 Answer 1

0

I don't know groovy but I think I can help.

instead of this:

result = [:]

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

You need something like this:

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)
    }
}
3
  • Thank you for the help , i am learning .
    – Asar
    Sep 20, 2022 at 7:50
  • Do you know how i can retrieve and branch and edit its image_name in the list?
    – Asar
    Sep 20, 2022 at 8:01
  • check out this: baeldung.com/groovy-maps
    – Omeerfk
    Sep 20, 2022 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.