I'm trying to concatenate a mutable with an immutable map in scala.
case class Result(val key: String, val number: Int)
object Test extends App {
val map1 = scala.collection.mutable.Map[String, List[Result]]()
val map2: Map[String, List[Result]] = Map("test" -> List(new Result("0", 0)))
map1 ++= map2.map(c => (c -> (c._2 + map1.getOrElse(c._1, List[Result]()))))
}
But compiler always says:
type mismatch; found : List[Result] required: String
When i change ListResult to "test" compiler says:
type mismatch; found : java.io.Serializable required: String
I'm quite confused. Do I use getOrElse the wrong way?
regards Cyrill
map on a Map gets passed each element of the Map. So c is a
(String, List[Result])
. So thisis ((String, List[Result), (List[Result]). Which is wrong. I imagine you meant
but that still has a type mismatch. I think you're hitting the fairly common problem of the implicit Any2String for +, and the compiler wants a String where it shouldn't.
Anyway,
seems to work
Also, Nil is a bit neater:
and clearer again (IMO) is the for comprehension