potatopotato
potatopotato

Reputation: 1164

Groovy, create map out of array

I have an array of elements and wanted to create a map out of it

def vegetable = ['carrot', 'salad']
def fruit = ['orange', 'apple']
def all_food = group_a + group_b

def my_map = [:]
for(item in all_food) {
    my_map.put(item, "edible")
}

any thought how to create it inline?

def my_map = all_food.each{it, "edible"}

or smth similar ?

Upvotes: 1

Views: 62

Answers (1)

Reimeus
Reimeus

Reputation: 159754

How about

def result = all_food.collectEntries {[it, "edible"]}

Upvotes: 3

Related Questions