Yogesh Patil Yogesh Patil - 4 months ago 32
Scala Question

Convert Scala map to java Dictionary

I am calling some Java API in my scala code. Java API is expecting input of type

and my data is in Scala Map collection which I need to convert to Java Dictionary before invoking Java API's.

So is there any way or converter to do this conversion?


This should do it:

scala> import collection.JavaConverters._
import collection.JavaConverters._

scala> val x = Map(1 -> 2, 3 -> 4)
x: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4)

scala> val a: java.util.Dictionary[Int, Int] = new java.util.Hashtable(x.asJava) 
a: java.util.Dictionary[Int,Int] = {3=4, 1=2}