Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

groovy convert the list into maps

Having this groovy script

def projectDirectories = [
    "golang-base",
    "java-base",
    "cpp-base",
    "ubuntu-fips",
    "suse-base",
    "golang-base",
    "java-base",
    "cpp-base",
    "ubuntu-fips",
    "suse-base",
]
def partition(array, size) {
    def partitions = []
    int partitionCount = array.size() / size

    partitionCount.times { partitionNumber ->
        def start = partitionNumber * size
        def end = start + size - 1
        partitions << array[start..end]
    }

    if (array.size() % size) partitions << array[partitionCount * size..-1]
    return partitions
}

def results = partition(projectDirectories, 2)
assert results instanceof List
for(i=0;i<results.size();i++)
{
    def resultUUID = UUID.randomUUID()
                 .toString()
                 .split('-')[-1..-2]
                 .join()
                 .toUpperCase()
    println (i+":"+resultUUID);

}

and it prints this result

[0:0D0759825372ADC2]
[1:E0E191BDFFD9B882]
[2:1AC6086897BA9FFE]
[3:8F92F1C0B70697B9]
[4:EC0D5D755849A01A]

Now, how to convert into maps like this :
[0:0D0759825372ADC2,1:E0E191BDFFD9B882,2:1AC6086897BA9FFE,3:8F92F1C0B70697B9,4:EC0D5D755849A01A]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Idiomatic Groovy would say:

def results = 1..5 // or your list
int ix = 0
Map map = results.collectEntries{ [ ix++, UUID.randomUUID().toString().split('-')[-1..-2].join().toUpperCase() ] }

returns

{
  "0": "4182AA9581C88EA1",
  "1": "F57F19859C1F8794",
  "2": "399BBA447F53A99B",
  "3": "33CCE45D78A3BED5",
  "4": "9D4B2782D7E982AF"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading