So I have a String which looks a little something like this:
text = "foo/bar;baz/qux"
My end goal is to split this String into a Multimap like this:
["level1" : ["foo", "baz"], "level2" : ["bar", "qux"]]
I also added Multimap-support to LinkedHashMap's metaClass:
LinkedHashMap.metaClass.multiPut << { key, value ->
delegate[key] = delegate[key] ?: []; delegate[key] += value
}
The String needs to be split at semi-colon and then again at forwardslash. Currently I'm populating my Multimap within a nested for-loop but obviously there's a Groovier way of doing this. Thus I was wondering what my options are?
I'm thinking something along the lines of:
def final myMap = text.split(';')
.collectEntries { it.split('/')
.eachWithIndex { entry, index -> ["level${index + 1}" : entry] }}
You can use withDefault on your returned Map to get rid of the ternary:
def text = "foo/bar;baz/qux;foo/bar/woo"
def result = text.split(';')*.split('/').inject([:].withDefault {[]}) { map, value ->
value.eachWithIndex { element, idx ->
map["level${idx+1}"] << element
}
map
}
assert result == [level1:['foo', 'baz', 'foo'], level2:['bar', 'qux', 'bar'], level3:['woo']]
If you don't want duplicates in your results, then you can use a Set in your withDefault (then convert back to a List afterwards):
def text = "foo/bar;baz/qux;foo/bar/woo"
def result = text.split(';')*.split('/').inject([:].withDefault {[] as Set}) { map, value ->
value.eachWithIndex { element, idx ->
map["level${idx+1}"] << element
}
map
}.collectEntries { key, value -> [key, value as List] }
assert result == [level1:['foo', 'baz'], level2:['bar', 'qux'], level3:['woo']]
My take on it, I wouldn't consider it very clever but I find it much easier to read:
def myMap = [:]
text.split(';').eachWithIndex{ entry, index ->
myMap << ["level${index + 1}": entry.split('/')]
}
If you are using Groovy 2.4.0 or above, you could use the withIndex() method which has been added to java.lang.Iterable:
def myMap = text.split(';').withIndex().collect{ entry, index ->
["level${index + 1}": entry.split('/')]
}
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?
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")
}
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} -> ${list}'
}
Gives:
a -> 1,2,3
The use of the injected multiPut() method does the magic.
You could also do something like this:
// Dummy map for testing
lineEdits = [ flag:[
[id:10001, mnemonic:'TRA', action:'review'],
[id:10002, mnemonic:'REB', action:'deny'],
[id:10003, mnemonic:'UNB', action:'deny'],
[id:20001, mnemonic:'REB', action:'deny'],
[id:20002, mnemonic:'ICD', action:'review'],
[id:30001, mnemonic:'REB', action:'deny'],
[id:40001, mnemonic:'ICD', action:'review'],
[id:40002, mnemonic:'MPR', action:'review'],
[id:50001, mnemonic:'CPT', action:'deny'],
[id:60001, mnemonic:'DTU', action:'deny'],
[id:70001, mnemonic:'ICD', action:'review'],
[id:70002, mnemonic:'MPR', action:'review'] ] ]
def editsMap = lineEdits.flag
.groupBy { it.id } // Group by id
.collectEntries { k, v ->
[ k, v[ 0 ] ] // Just grab the first one (flatten)
}
assert editsMap[ 60001 ] == [ id:60001, mnemonic:'DTU', action:'deny' ]
If you want to do the multimap thing without external classes, you can just store a map of lists instead, the syntax won't be cumbersome or anything.
def editsMap = [:].withDefault{[]}
lineEdits.flag.each
{
lineEdits_Flag ->
editsMap.FlagId << lineEdits_Flag.id
editsMap.FlagMnemonic << lineEdits_Flag.mnemonic
editsMap.Action << lineEdits_Flag.action
println "editsMap: ${editsMap}"
}
or if you really preferred your original syntax it would look like:
editsMap.get('FlagId').add(lineEdits_Flag.id)
or even this should work:
editsMap.get('FlagId') << lineEdits_Flag.id
The advantage of this solution is that it tends to be more obvious what you are doing... for instance it's not a magic map that converts single items to a list (which is not the standard map contract) but it's always a map of lists that you simply use as a map of lists.
The .get will always work the same way the multimap was described--it will always return the list for that item in the map.
A map is a set of key-value mappings, you plug in different values by key so that you can use the key to find them later. Your example is plugging in values for the same keys over and over. You need to pick unique keys.
Make some class to store your values for one entry in the map:
class Stuff {
String flagMnemonic
String action
}
Make a map where you will use flagId as the key (because that's how you identify the flag uniquely) and Stuff as the value (because it's the data you want to lookup).
def editsMap = [:]
If you used type declarations here, and if flagId is a String, the map's type would be Map<String, Stuff>.
Now you can put stuff in the map:
lineEdits.flag.each { lineEdits_Flag ->
editsMap[lineEdits_Flag.id] =
new Stuff(
flagMnemonic: lineEdits_Flag.mnemonic,
action: lineEdits_Flag.action)
}
and get it back out with
def myStuffFor10001 = editsMap['10001']
println myStuffFor10001.flagMnemonic // should equal 'TRA'
println myStuffFor10001.action // should equal 'review'
Also there's an easy alternative to using ?: "default" to set default values, you can use withDefault when creating your map:
def defaultStuff = new Stuff(
flagMnemonic: "defaultMnemonic", action:"defaultAction")
def editsMap = [:].withDefault { defaultStuff }
so that whenever you ask for something from the map that is not present there, you get the specified default object.
Why not use a list and closure like:
editsList = [
[FlagId:10001, FlagMnemonic:TRA, Action:review],
[FlagId:10002, FlagMnemonic:REB, Action:deny],
[FlagId:10003, FlagMnemonic:UNB, Action:deny],
[FlagId:20001, FlagMnemonic:REB, Action:deny],
[FlagId:20002, FlagMnemonic:ICD, Action:review],
[FlagId:30001, FlagMnemonic:REB, Action:deny],
[FlagId:40001, FlagMnemonic:ICD, Action:review],
[FlagId:40002, FlagMnemonic:MPR, Action:review],
[FlagId:50001, FlagMnemonic:CPT, Action:deny],
[FlagId:60001, FlagMnemonic:DTU, Action:deny],
[FlagId:70001, FlagMnemonic:ICD, Action:review],
[FlagId:70002, FlagMnemonic:MPR, Action:review]
]
def appliedEditsMap = {property,idValue->
return editsList.find{it[property] == idValue}
}
def thisValue = appliedEditsMap(FlagId, '10001') ?: "default"
You need to put this in to a class and then add that class in to the Map. From what I see your information is related so having a class to store makes sense unless I'm missing anything
What you can do is define your class as
class Flag {
String flagID
String flagMnemonic
String action
}
Now Put your Flag in to your map as
editsMap.put(10000,newFlag(flagID:'10000',flagMnemonic:'TES',action:'tes'))
I had a similar issue recently and I knew it was possible because some coworkers had done it. Reading the answers here and experimenting, I finally found a simple answer that worked for my use-case and isn't difficult to read. Writing a "generic code" answer makes this a little less readable than it is in our code with the proper column names, etc...
In my case, I was getting a List<Map> back from a repo query; I needed something like Map<String, List<Object>> and I need to add to the List if a result set's key matched a previous one. Of course, my Object wasn't a POJO, but you can use any Class. And to further complicate it, I needed to create a composite key from a few of the result values (don't ask, I didn't create it) and remove those keys from the original Map so I could use the remaining entries to create a business Object.
Here's what I did:
List<Map> listMap = repo.findWhateverItWas()
Map<String, List<Object>> resultMap = [:].withDefault {[]} //required to avoid NPE
listMap.each { Map<String, Object> it ->
String compKey = it['col1'] + it['col2'] + it['col3']
Map tempMap =[:]
it.keySet.each { k ->
if (!(k in ['col1','col2','col3'])) {
tempMap << [(k): it[k]] // this is the business Object result map
}
}
// createEntity is a static Entity Factory
// the simple += is the magic
resultMap[(compKey)] += createEntity(Entity.class, tempMap)
}
return resultMap
I realize this doesn't address your specific scenario, but I do believe it answers the question and provides an answer for a more complex situation.
I was able to prove the expected functionality of this with a simple test case. We use Spock...
def "Map of Lists test"() {
given:
Map<String, List<String>> map = [:].withDefault { [] }
when:
map['key1'] += 'item1'
map['key1'] += 'item2'
map['key2'] += 'item3'
map['key1'] += 'item4'
map['key2'] += 'item5'
then:
map['key1'] == ['item1', 'item2', 'item4']
map['key2'] == ['item3', 'item5']
}
I'm trying to write a trait (in Scala 2.8) that can be mixed in to a case class, allowing its fields to be inspected at runtime, for a particular debugging purpose. I want to get them back in the order that they were declared in the source file, and I'd like to omit any other fields inside the case class. For example:
trait CaseClassReflector extends Product {
def getFields: List[(String, Any)] = {
var fieldValueToName: Map[Any, String] = Map()
for (field <- getClass.getDeclaredFields) {
field.setAccessible(true)
fieldValueToName += (field.get(this) -> field.getName)
}
productIterator.toList map { value => fieldValueToName(value) -> value }
}
}
case class Colour(red: Int, green: Int, blue: Int) extends CaseClassReflector {
val other: Int = 42
}
scala> val c = Colour(234, 123, 23)
c: Colour = Colour(234,123,23)
scala> val fields = c.getFields
fields: List[(String, Any)] = List((red,234), (green,123), (blue,23))
The above implementation is clearly flawed because it guesses the relationship between a field's position in the Product and its name by equality of the value on those field, so that the following, say, will not work:
Colour(0, 0, 0).getFields
Is there any way this can be implemented?
Look in trunk and you'll find this. Listen to the comment, this is not supported: but since I also needed those names...
/** private[scala] so nobody gets the idea this is a supported interface.
*/
private[scala] def caseParamNames(path: String): Option[List[String]] = {
val (outer, inner) = (path indexOf '$') match {
case -1 => (path, "")
case x => (path take x, path drop (x + 1))
}
for {
clazz <- getSystemLoader.tryToLoadClass[AnyRef](outer)
ssig <- ScalaSigParser.parse(clazz)
}
yield {
val f: PartialFunction[Symbol, List[String]] =
if (inner.isEmpty) {
case x: MethodSymbol if x.isCaseAccessor && (x.name endsWith " ") => List(x.name dropRight 1)
}
else {
case x: ClassSymbol if x.name == inner =>
val xs = x.children filter (child => child.isCaseAccessor && (child.name endsWith " "))
xs.toList map (_.name dropRight 1)
}
(ssig.symbols partialMap f).flatten toList
}
}
Here's a short and working version, based on the example above
trait CaseClassReflector extends Product {
def getFields = getClass.getDeclaredFields.map(field => {
field setAccessible true
field.getName -> field.get(this)
})
}
In every example I've seen the fields are in reverse order: the last item in the getFields array is the first one listed in the case class. If you use case classes "nicely", then you should just be able to map productElement(n) onto getDeclaredFields()( getDeclaredFields.length-n-1).
But this is rather dangerous, as I don't know of anything in the spec that insists that it must be that way, and if you override a val in the case class, it won't even appear in getDeclaredFields (it'll appear in the fields of that superclass).
You might change your code to assume things are this way, but check that the getter method with that name and the productIterator return the same value and throw an exception if they don't (which means that you don't actually know what corresponds to what).
You can also use the ProductCompletion from the interpreter package to get to attribute names and values of case classes:
import tools.nsc.interpreter.ProductCompletion
// get attribute names
new ProductCompletion(Colour(1, 2, 3)).caseNames
// returns: List(red, green, blue)
// get attribute values
new ProductCompletion(Colour(1, 2, 3)).caseFields
Edit: hints by roland and virtualeyes
It is necessary to include the scalap library which is part of the scala-lang collection.
Thanks for your hints, roland and virtualeyes.