2

I have a two Maps and I want to concatenate them.

I tried almost all example given here Best way to merge two maps and sum the values of same key? but it ignores all values for key metrics and only stores last value.

I have downloaded scalaz-full_2.9.1-6.0.3.jar and imported it import scalaz._ but it won't works for me. How can I concate this two maps with multiple values to same keys ?

Edit :-

Now I tried

  val map = new HashMap[String, Set[String]] with MultiMap[String, String]
  map.addBinding("""report_type""" , """performance""")
  map.addBinding("""start_date""" ,start_date)
  map.addBinding("""end_date""" , end_date)
  map.addBinding("metrics" , "plays")
  map.addBinding("metrics", "displays")
  map.addBinding("metrics" , "video_starts")
  map.addBinding("metrics" , "playthrough_25")
  map.addBinding("metrics", "playthrough_50")
  map.addBinding("metrics", "playthrough_75")
  map.addBinding("metrics", "playthrough_100")

  val map1 = new HashMap[String, Set[String]] with MultiMap[String, String]
  map1.addBinding("""dimensions""" , """asset""")
  map1.addBinding("""limit""" , """50""")

And tried to conver this mutable maps to immutable type using this link as

val asset_query_string = map ++ map1
val asset_query_string_map =(asset_query_string map { x=> (x._1,x._2.toSet) }).toMap[String, Set[String]]

But still I get

i_ui\config\config.scala:51: Cannot prove that (String, scala.collection.immutable.Set[String]) <:< (St
ring, scala.collection.mutable.Set[String]).
11:10:13.080 [ERROR] i.g.a.ZincCompiler$ - val asset_query_string_map =(asset_query_string map { x=> (x
._1,x._2.toSet) }).toMap[String, Set[String]]
Community
  • 1
  • 1
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92

3 Answers3

4

Your problem is not related with a concatenation but with a declaration of the metrics map. It's not possible to have multiple values for a single key in a Map. Perhaps you should look at this collection:

http://www.scala-lang.org/api/2.10.3/index.html#scala.collection.mutable.MultiMap

Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65
1

You can't have duplicate keys in a Map.

for simple map it is impossible to have duplicates keys,if you have the duplicates keys in the map it takes the last one

but you can use MultiMap

import collection.mutable.{ HashMap, MultiMap, Set }
val mm = new HashMap[String, Set[String]] with MultiMap[String, String]
mm.addBinding("metrics","plays")
mm.addBinding("metrics","displays")
mm.addBinding("metrics","players")
println(mm,"multimap")//(Map(metrics -> Set(players, plays, displays)),multimap)
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
0

I was able to create two MultiMaps but when I tried to concatenate val final_map = map1 ++ map2 and I tried answer given here Mutable MultiMap to immutable Map

But my problem was not solved, I got

config\config.scala:51: Cannot prove that (String, scala.collection.immutable.Set[String]) <:< (St
ring, scala.collection.mutable.Set[String]).

finally it solved by

val final_map = map1 ++ map2
val asset_query_string_map = final_map.map(kv => (kv._1,kv._2.toSet)).toMap
Community
  • 1
  • 1
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92