Suppose I have a map m: Map[Any, Int]
. Now I would like to take only entries (String, Int)
from m
and create a new map m1: Map[String, Int]
with those entries.
I am trying to do the following:
val m1: Map[String, Int] = m collect {case e:(String, Int) => e}
It seems working but I get a warning: non variable type-argument String in type pattern (String, Int) is unchecked since it is eliminated by erasure.
How can I get rid of the warning?
you probably want:
val m1: Map[String, Int] = m collect {case (k:String, v:Int) => k->v}
(Just for reference. What you want is virtualeyes’s answer.)
val m1: Map[String, Int] = m flatMap { e =>
e._1 match {
case e1: String => Some(e1 -> e._2)
case _ => None
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With