How can I convert Scala Map to Java Map with scala.Float to java.Float k/v conversion

前端 未结 4 799
梦如初夏
梦如初夏 2020-12-14 05:55

I would like to be able to perform the following, but it fails in the call to useMap. How can I perform this conversion?

scala> import scala.collection.Ja         


        
4条回答
  •  悲哀的现实
    2020-12-14 06:27

    scala> v.asJava
    :16: error: type mismatch;
     found   : java.util.Map[Int,scala.Float]
     required: java.util.Map[Integer,java.lang.Float]
    

    The type of v is Map[scala.Int, scala.Float], not Map[java.lang.Integer, java.lang.Float].

    You could try this:

    import java.lang.{Float => JFloat}
    useMap(v.map{ case (k, v) => (k: Integer) -> (v: JFloat)})
    

提交回复
热议问题