英文:
Groovy, create map out of array
问题
我有一个元素数组,想要将其转换为映射。
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")
}
有没有什么内联的方式来创建它呢?
def my_map = all_food.collectEntries{[it, "edible"]}
或者类似的方法?
英文:
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 ?
答案1
得分: 3
关于这部分代码:
def result = all_food.collectEntries {[it, "可食"]}
英文:
How about
def result = all_food.collectEntries {[it, "edible"]}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论