maps

How to convert between Scala and Java maps

Scala/Java integration problem: You need to share a Scala map with a Java method, or access a Java map in Scala code.

Solution

To use a Java map in Scala code, import the mapAsScalaMap method from Scala’s JavaConversions package, and perform the conversion.

Simply trying to use a Java HashMap with something like a Scala for loop won’t work. To demonstrate this, first create a simple Java HashMap:

Scala Map class examples - mapping month numbers to names

Nothing too earth shattering here today, but if you need an example of the Scala Map class syntax (how to create a Scala Map), or just want to copy and paste a map of month names to numbers (or numbers to names), I hope the following code is helpful: