Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Java to Scala explicit type casting on unknown java.util.Map type

I recently worked with some Java collection (well know JavaFX), and I had recently a problem (a consequence of this other problem posted here). One of the JavaFX interfaces I need only accepts java.util.Map, equal to Map[_,_] in Scala.

I make a conversion with asInstanceOf, but after computation, if I want to transform my java.util.Map[_,_] to force the cast into a real Scala type safe Map[String,Double] I use in all my program, how can I do it?

I tried java.conversions._ and asInstanceOf methods without success.

//return a java.util.Map 
val row: java.util.Map[_,_] = c.getTableView().getItems().get(0)
//I need a Map[String,Double] in my program
val parameters = row.toMap[String,Double]
like image 304
reyman64 Avatar asked Nov 22 '25 01:11

reyman64


1 Answers

This works for me

val javaMap : java.util.Map[_, _] = null
val scalaMap = javaMap.asScala.toMap.asInstanceOf[Map[String, Double]]

Remember to include

import scala.collection.JavaConverters._
like image 101
sksamuel Avatar answered Nov 23 '25 15:11

sksamuel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!