Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add multiple Groovy map entries without overwriting the current entries?

Tags:

map

groovy

My question with Groovy Maps. I've been searching for a way to programmatically add a new entry to a Groovy map without overwriting the current entry. For example

def editsMap = [:]

lineEdits.flag.each 
{ lineEdits_Flag ->
   editsMap.put('FlagId',lineEdits_Flag.id)
   editsMap.put('FlagMnemonic',lineEdits_Flag.mnemonic)
   editsMap.put('Action',lineEdits_Flag.action)   
   println "editsMap: ${editsMap}"
}

The first pass produces this map:
editsMap: [FlagId:10001, FlagMnemonic:TRA, Action:review]

But the second pass overwrites the first pass with: editsMap: [FlagId:10002, FlagMnemonic:REB, Action:deny]

What I'm trying to do is create multiple entries within the one map. I need my map to populate something like this:

editsMap: [FlagId:10001, FlagMnemonic:TRA, Action:review]
editsMap: [FlagId:10002, FlagMnemonic:REB, Action:deny]
editsMap: [FlagId:10003, FlagMnemonic:UNB, Action:deny]
editsMap: [FlagId:20001, FlagMnemonic:REB, Action:deny]
editsMap: [FlagId:20002, FlagMnemonic:ICD, Action:review]
editsMap: [FlagId:30001, FlagMnemonic:REB, Action:deny]
editsMap: [FlagId:40001, FlagMnemonic:ICD, Action:review]
editsMap: [FlagId:40002, FlagMnemonic:MPR, Action:review]
editsMap: [FlagId:50001, FlagMnemonic:CPT, Action:deny]
editsMap: [FlagId:60001, FlagMnemonic:DTU, Action:deny]
editsMap: [FlagId:70001, FlagMnemonic:ICD, Action:review]
editsMap: [FlagId:70002, FlagMnemonic:MPR, Action:review]

Once I have populated my map then I need to be able to find certain values in order to process a message. I believe that I can use something like:

def thisValue = appliedEditsMap[FlagId, '10001'] ?: "default"

to do a quick lookup.

Can someone help me understand how to programmatically add values to a Groovy map without overwriting the values already in the map?

like image 460
kbhuffaker Avatar asked Mar 26 '13 15:03

kbhuffaker


2 Answers

You want something like Guava's MultiMap:

Multimap<String, String> myMultimap = ArrayListMultimap.create();

// Adding some key/value
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");

// Getting values
Collection<string> fruits = myMultimap.get("Fruits");
System.out.println(fruits); // [Bannana, Apple, Pear]

This guy makes a pure Groovy emulation of Multimap:

class GroovyMultimap {
    Map map = [:]

    public boolean put(Object key, Object value) {
        List list = map.get(key, [])
        list.add(value)
        map."$key" = list
    }
}

You can use putAt and getAt for syntatic sugar in map operations. You can also try a mixin in a map object.

He also uses Groovy with Guava's multimap:

List properties = ['value1', 'value2', 'value3']
Multimap multimap = list.inject(LinkedListMultimap.create()) {
    Multimap map, object ->
    properties.each {
        map.put(it, object."$it")
    }
    map
}
properties.each {
    assertEquals (multimap.get(it), list."$it")
}
like image 193
Will Avatar answered Feb 05 '23 08:02

Will


I came across this several years ago as an answer to a similar question on another site. I can't find where it originally came from so if anyone knows the source please post it here.

LinkedHashMap.metaClass.multiPut << { key, value ->
    delegate[key] = delegate[key] ?: []; delegate[key] += value
}

def myMap = [:]

myMap.multiPut("a", "1")
myMap.multiPut("a", "2")
myMap.multiPut("a", "3")

myMap.each {key, list ->
    println "${key} -> $value.list(",")
}

Gives:

a -> 1,2,3

The use of the injected multiPut() method does the magic.

like image 26
rifboy Avatar answered Feb 05 '23 06:02

rifboy