I am trying to filter a ListMap
with values in a List
. But the order is not maintained in output mutable.ListMap
. This is a simplified code. As there are constraints and validation checks to be done, I cant do this with filter
.
Here is my code,
val inMap = scala.collection.immutable.ListMap((1,5),(2,4),(3,5),(7,6))
val alist= List(1,2,3)
val mutableTempMap = scala.collection.mutable.ListMap.empty[Int, Int]
for (jkey <- alist) {
inMap.get(jkey) match {
case Some(y) => mutableTempMap(jkey) = y
case None =>
}
}
mutableTempMap
Output:
scala.collection.mutable.ListMap[Int,Int] = Map(3 -> 5, 1 -> 5, 2 -> 4)
Expected Output
scala.collection.mutable.ListMap[Int,Int] = Map(1 -> 5, 2 -> 4, 3 -> 5)