Yogesh Patil
Yogesh Patil

Reputation: 586

Convert Scala map to java Dictionary

I am calling some Java API in my scala code. Java API is expecting input of type java.util.Dictionary 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?

Upvotes: 1

Views: 191

Answers (1)

aviaviavi
aviaviavi

Reputation: 308

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}

Upvotes: 4

Related Questions