Reputation: 26473
I have two lists:
def ids = [1L, 2L, 3L]
def values = [11, 12, 13]
I would like to create a HashMap
with ids
as keys and values
as values.
I've tried to use transpose
but stucked with GroovyCastException
Upvotes: 7
Views: 2288
Reputation: 42194
GroovyCollections.transpose(lists)
"zips" elements from two lists, e.g.
[[1,2], [3,4]].transpose() == [[1,3], [2,4]]
You can use it in combination with .collectEntries()
to create a map from such output:
Map map = [ids, values].transpose().collectEntries()
assert map == [1: 11, 2: 12, 3: 13]
It will create a map like:
[1:11, 2:12, 3:13]
using your input data.
Upvotes: 13