The outer map (amap
) is creating a single instance of the inner map to use as the default. When you access this via val bmap = amap(2)
, then modify bmap
, you are modifying the single default map used by amap
. When you call amap(3)
, you then get back this default map, which is now a map with the key/value pair (1 -> "something")
.
What you probably want is withDefault
, not withDefaultValue
, although it needs some extra argument/type specification to work:
val amap = Map[Int, MMap[Int, String]]().withDefault(x => MMap[Int, String]().withDefaultValue("default"))