将键作为字符串映射,并将值作为列表在Groovy中 [英] Map with Key as String and Value as List in Groovy

查看:192
本文介绍了将键作为字符串映射,并将值作为列表在Groovy中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以指出一个例子,说明如何在Groovy中使用一个 Map ,其中有一个 String 作为它的键和一个列表作为值?

解决方案

Groovy接受几乎所有的Java语法,所以有一系列选择,如下所示:

  // Java语法

地图<字符串,列表> map1 = new HashMap< String,List>();
列表list1 = new ArrayList();
list1.add(hello);
map1.put(abc,list1);
assert map1.get(abc)== list1;

//略少Java-esque

def map2 = new HashMap< String,List>()
def list2 = new ArrayList()
list2.add(hello)
map2.put(abc,list1)
assert map2.get(abc)== list2

//典型的Groovy

def map3 = [:]
def list3 = []
list3<< hello
map3.'abc'= list1
assert map3.'abc'== list3


Can anyone point me to an example of how to use a Map in Groovy which has a String as its key and a List as value?

解决方案

Groovy accepts nearly all Java syntax, so there is a spectrum of choices, as illustrated below:

// Java syntax 

Map<String,List> map1  = new HashMap<String,List>();
List list1 = new ArrayList();
list1.add("hello");
map1.put("abc", list1); 
assert map1.get("abc") == list1;

// slightly less Java-esque

def map2  = new HashMap<String,List>()
def list2 = new ArrayList()
list2.add("hello")
map2.put("abc", list1)
assert map2.get("abc") == list2

// typical Groovy

def map3  = [:]
def list3 = []
list3 << "hello"
map3.'abc'= list1
assert map3.'abc' == list3

这篇关于将键作为字符串映射,并将值作为列表在Groovy中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆