score:30
Use scala predefined float2Float
and use CollectionConverters to perform conversion explicitly.
import scala.jdk.CollectionConverters._
// Prior to Scala 2.13: import scala.collection.JavaConverters._
val scalaMap = Map(1 -> 1.0F)
val javaMap = scalaMap.map{ case (k, v) => k -> float2Float(v) }.asJava
score:3
scala> v.asJava
<console>: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)})
score:11
Implicit conversions are sometimes hard to debug/understand and therefore I prefer explicit conversions as follows:
import scala.collection.JavaConversions.mapAsJavaMap
val scalaMap = Map(1 -> 1.0F)
val javaMap = mapAsJavaMap(scalaMap)
score:45
The solution linked to by @pagoda_5b works: Scala convert List[Int] to a java.util.List[java.lang.Integer]
scala> import collection.JavaConversions._
import collection.JavaConversions._
scala> val m = mapAsJavaMap(Map(1 -> 2.1f)).asInstanceOf[java.util.Map[java.lang.Integer, java.lang.Float]]
m: java.util.Map[Integer,Float] = {1=2.1}
scala> m.get(1).getClass
res2: Class[_ <: Float] = class java.lang.Float
Source: stackoverflow.com
Related Query
- How can I convert Scala Map to Java Map with scala.Float to java.Float k/v conversion
- How can I convert a Java Iterable to a Scala Iterable?
- Spark: How to map Python with Scala or Java User Defined Functions?
- How can I convert scala Map to a partial function
- How can I convert a Java map of maps for use in Scala?
- How to convert java Map to scala Map of type LinkedHashMap[String,ArrayList[String]]?
- How to convert a java Map to immutable Scala map via java code?
- How can i compile java record with scala code?
- How to convert Tuples to Map with Set in Scala
- How to un/marshall a Float as a Scala Option[Float] rather than a Java Float with JAXB?
- How to convert Java Map of Lists to Scala Map of Lists
- Scala Spark How can I convert a column array[string] to a string with JSON array in it?
- Convert Java map into Scala immutable map in Java code with Scala-2.13
- How to convert a java HashMap to immutable Scala map via java code?
- How to convert an Iterator[int] to a map with frequency per bin in scala
- How can I match constructor types with Java reflection with Scala primitive types?
- How to convert Java Map containing List to Scala map
- How do I can convert Scala for loop into Java
- How to convert a nested complex scala Map to Java
- How can I copy a Java map to scala map using foreach
- In Scala, how can I subclass a Java class with multiple constructors?
- Convert Java Map to Scala Map
- How to implement Map with default operation in Scala
- How to convert a Java Stream to a Scala Stream?
- How to convert Scala Map into JSON String?
- How can one provide manually specialized implementations with Scala specialization?
- How can I convert a json string to a scala map?
- How to convert a nested scala collection to a nested Java collection
- How to convert from from java.util.Map to a Scala Map
- How to convert a Scala Array[Byte] to Java byte[]?
More Query from same tag
- Is there an easy way to get a Stream as output of a RowParser?
- How to stop an Actor reloading on exception
- Scala Spark how to use --files
- Play framework 2.5 scala i18n
- Why does this Spark code throw java.io.NotSerializableException
- Traversable => Java Iterator
- Scala Slick: MTable.getTables return empty vector/list
- String interpolation and macro: how to get the StringContext and expression locations
- matching in scala map to an object
- java.lang.ClassCastException: models.Task cannot be cast to models.Task on Play Framework
- Get sum of combinations of array values in scala
- Is it possible to write a repeated-parameter function through function literal in scala ?
- Scala: filter Seq[] (make a diff)
- How to deal with this kind of log contains json
- How reuse class parameter in case classes?
- Matrix Multiplication in Apache Spark
- Reading Nested JSON via Spark SQL - [AnalysisException] cannot resolve Column
- Reference in documentation to ->>>[_, _ ] and ~>
- Does Scala's immutability mean the list can't be modified or the list and its contents can't be modified?
- Scala: create a dependent type variable from Any
- Drop column(s) in spark csv data frame
- ReactiveMongo 0.9: Joda Datetime Implicit Conversion for Macros.handler
- Scala object for importing all methods
- LessThan in Scala with 'Ordered*' Types
- Play! Framework (2.6.2) passing route with type Call to custom view
- How to update a nested column in spark DataFrame
- Initialising vals which might throw an exception
- Insert a CSV file to SQL server using scala
- Scala: sort comparing with adjacent elements
- how to convert rows into columns in spark dataframe, scala