简体   繁体   English

Groovy 2 List 转换成 Map(String,List<String> )

[英]Groovy 2 List convert into a Map(String,List<String>)

I have 2 List, details are below我有 2 个列表,详细信息如下

def value =['a','b','c']
def recId =['R1','R2']

I need to create a single Map (string, List) with the above two lists, like我需要使用上述两个列表创建一个 Map (string, List),例如

[R1:[a,b,c]
R2:[a,b,c]]

can anyone suggest me the solution?谁能建议我解决方案?

def value =['a','b','c']
def recId =['R1','R2']

def map = recId.collectEntries{e-> [e,value] }

but each entry will reference the same value array.但每个条目将引用相同的value数组。 so, changing one of them will change all.所以,改变其中之一将改变所有。

the following code will create a copy of value list for each entry以下代码将为每个条目创建value列表的副本

def map = recId.collectEntries{e-> [e,value.clone()] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM