为迭代列表生成每个映射键的列表?

时间:2021-09-18 00:44:43

I have a map and i need to insert a generated list to each map key in groovy as i am iterating through a list !

我有一个地图,我需要在groovy中为每个地图键插入一个生成的列表,因为我正在迭代列表!

Code:

def myMap = [:]
    anotherList.each{
    object -> //here i do some work to get two elements
    def element1 = ..
    def element2 = ..

    // so here i need to generate a list for the two elements with index 0 and 1
    myMap[obejct]= ['list', my list] 
    }
   return myMap

2 个解决方案

#1


You can create a map using [ key: value] notation. Since your value is an array of two elements you can simply create it using [element1,element2] notation, and then you can add the object to the map using << operator.

您可以使用[key:value]表示法创建地图。由于您的值是两个元素的数组,您可以使用[element1,element2]表示法创建它,然后您可以使用< <运算符将对象添加到地图中。< p>

So this myMap << [ (object) : [element1,element2]] can do the job.

所以这个myMap << [(object):[element1,element2]]可以完成这项工作。

In your code:

在你的代码中:

   def myMap = [:]
    anotherList.each{
    object -> //here i do some work to get two elements
    def element1 = ..
    def element2 = ..

    // so here i need to generate a list for the two elements with index 0 and 1
     myMap << [ (object) :  [element1,element2]]
    }
   return myMap

Note that I use (object) to evaluate the key, because if I use directly object the literal is used as key in the map instead of the value.

请注意,我使用(object)来计算键,因为如果我直接使用对象,则将文字用作映射中的键而不是值。

Hope this helps,

希望这可以帮助,

UPDATED BASED ON OP COMMENT:

基于OP评论更新:

If I understand well your requirements you want that map keys are the index instead of the value isn't it? To do so you can use eachWithIndex instead of each for example:

如果我理解你的要求你想要那个地图键是索引而不是价值不是吗?为此,您可以使用eachWithIndex而不是每个例如:

def myMap = [:]
  def anotherList = ['a','b','c','d']
    anotherList.eachWithIndex{ object,index -> //here i do some work to get two elements
    def element1 = object + 'z'
    def element2 = object + 'x'

    // so here i need to generate a list for the two elements with index 0 and 1
     myMap << [ (index) :  [element1,element2]]
    }
   return myMap //Result: [0:[az, ax], 1:[bz, bx], 2:[cz, cx], 3:[dz, dx]]

#2


You can use the method collect to go through your list and generate another list whose values depend on the original list (the reasoning you use inside this method is up to you. Here a small example:

您可以使用collect方法浏览列表并生成另一个列表,其值取决于原始列表(您在此方法中使用的推理由您决定。这里有一个小例子:

    def originalList = [1,2,3]
    def result = originalList.collect{obj->
        def e1 = obj
        def e2 = obj*2
        [e1,e2]
    }
    println result //[[1,1],[2,4],[3,6]]

EDIT: Sorry, I overlooked the fact that you expect a map as result. Here is an approach similar to the one described above. In this case you use each to go through the elements of your list:

编辑:对不起,我忽略了你期望得到一张地图的事实。这是一种类似于上述方法的方法。在这种情况下,您可以使用每个元素遍历列表中的元素:

def originalList = [[name: 'franz', age: 12], [name: 'bepi', age: 20],[name: 'giovanni', age: 65]]
def result=[:]
originalList.each{obj->
    def e1 = obj.age
    def e2 = obj.age*2
    result.put(obj, [e1,e2]) //key, value
}
println result //[[name:franz, age:12]:[12, 24], [name:bepi, age:20]:[20, 40], [name:giovanni, age:65]:[65, 130]]

#1


You can create a map using [ key: value] notation. Since your value is an array of two elements you can simply create it using [element1,element2] notation, and then you can add the object to the map using << operator.

您可以使用[key:value]表示法创建地图。由于您的值是两个元素的数组,您可以使用[element1,element2]表示法创建它,然后您可以使用< <运算符将对象添加到地图中。< p>

So this myMap << [ (object) : [element1,element2]] can do the job.

所以这个myMap << [(object):[element1,element2]]可以完成这项工作。

In your code:

在你的代码中:

   def myMap = [:]
    anotherList.each{
    object -> //here i do some work to get two elements
    def element1 = ..
    def element2 = ..

    // so here i need to generate a list for the two elements with index 0 and 1
     myMap << [ (object) :  [element1,element2]]
    }
   return myMap

Note that I use (object) to evaluate the key, because if I use directly object the literal is used as key in the map instead of the value.

请注意,我使用(object)来计算键,因为如果我直接使用对象,则将文字用作映射中的键而不是值。

Hope this helps,

希望这可以帮助,

UPDATED BASED ON OP COMMENT:

基于OP评论更新:

If I understand well your requirements you want that map keys are the index instead of the value isn't it? To do so you can use eachWithIndex instead of each for example:

如果我理解你的要求你想要那个地图键是索引而不是价值不是吗?为此,您可以使用eachWithIndex而不是每个例如:

def myMap = [:]
  def anotherList = ['a','b','c','d']
    anotherList.eachWithIndex{ object,index -> //here i do some work to get two elements
    def element1 = object + 'z'
    def element2 = object + 'x'

    // so here i need to generate a list for the two elements with index 0 and 1
     myMap << [ (index) :  [element1,element2]]
    }
   return myMap //Result: [0:[az, ax], 1:[bz, bx], 2:[cz, cx], 3:[dz, dx]]

#2


You can use the method collect to go through your list and generate another list whose values depend on the original list (the reasoning you use inside this method is up to you. Here a small example:

您可以使用collect方法浏览列表并生成另一个列表,其值取决于原始列表(您在此方法中使用的推理由您决定。这里有一个小例子:

    def originalList = [1,2,3]
    def result = originalList.collect{obj->
        def e1 = obj
        def e2 = obj*2
        [e1,e2]
    }
    println result //[[1,1],[2,4],[3,6]]

EDIT: Sorry, I overlooked the fact that you expect a map as result. Here is an approach similar to the one described above. In this case you use each to go through the elements of your list:

编辑:对不起,我忽略了你期望得到一张地图的事实。这是一种类似于上述方法的方法。在这种情况下,您可以使用每个元素遍历列表中的元素:

def originalList = [[name: 'franz', age: 12], [name: 'bepi', age: 20],[name: 'giovanni', age: 65]]
def result=[:]
originalList.each{obj->
    def e1 = obj.age
    def e2 = obj.age*2
    result.put(obj, [e1,e2]) //key, value
}
println result //[[name:franz, age:12]:[12, 24], [name:bepi, age:20]:[20, 40], [name:giovanni, age:65]:[65, 130]]