Q:

Failed to add element to new ArrayList in Groovy

I am trying to simulate a multimap, each value of langVarMap is a list. When I add a new String to the list, I get the following error:
No signature of method: java.lang.Boolean.add() is applicable for argument types: (java.lang.String) values: [mm]

Here is the code snippet:              
  def langs = engine.languages as Set
                def langVarMap = [:] 
                engine.models.each { model -> 
                    def lang = (model.@language.text())   // String
                    def variant = (model.@variant.text()) // String
                    langs.add(lang)

                if (langVarMap.get(lang)) {
                    def a = langVarMap.get(lang)  //ArrayList               
                    langVarMap.put(lang, a.add(variant))
                    }
                else {
                    langVarMap.put(lang, [variant])
                    }

Thanks in advance.

A:

Problem is with this line:
langVarMap.put(lang, a.add(variant))

ArrayList.add(E e) returns boolean not the list. Adding result of add action adds a boolean value of TRUE to the map after which cannot call add method on it. Need to rewrite as following:
if (langVarMap.get(lang)) {
 def a = langVarMap.get(lang)  //ArrayList
 // a is already in langVarMap so don't need to put into ma again
 a.add(variant)
} else {
 langVarMap.put(lang, [variant])
}

And can further refine with this to remove the redundant lookup.
def a = langVarMap.get(lang)  //ArrayList
if (a) {
 a.add(variant)
} else {
 langVarMap.put(lang, [variant])
}

