Map with Key as String and Value as List in Groovy

前端 未结 5 612
慢半拍i
慢半拍i 2021-02-03 18:54

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?

5条回答
  •  醉酒成梦
    2021-02-03 19:20

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

    // Java syntax 
    
    Map map1  = new HashMap<>();
    List list1 = new ArrayList();
    list1.add("hello");
    map1.put("abc", list1); 
    assert map1.get("abc") == list1;
    
    // slightly less Java-esque
    
    def map2  = new HashMap()
    def list2 = new ArrayList()
    list2.add("hello")
    map2.put("abc", list2)
    assert map2.get("abc") == list2
    
    // typical Groovy
    
    def map3  = [:]
    def list3 = []
    list3 << "hello"
    map3.'abc'= list3
    assert map3.'abc' == list3
    

提交回复
热议问题