Question

I use a lot of scala maps, occasionally I want to pass them in as a map to a legacy java api which wants a java.util.Map (and I don't care if it throws away any changes).

Was it helpful?

Solution

An excellent library I have found that does a better job of this:

http://github.com/jorgeortiz85/scala-javautils

(bad name, awesome library). You explicitly invoke .asJava or .asScala depending on what direction you want to go. No surprises.

OTHER TIPS

Scala provides wrappers for Java collections so that they can be used as Scala collections but not the other way around. That being said it probably wouldn't be hard to write your own wrapper and I'm sure it would be useful for the community. This question comes up on a regular basis.

This question and answer discuss this exact problem and the possible solutions. It advises against transparent conversions as they can have very strange side-effects. It advocates using scala-javautils instead. I've been using them in a large project for a few months now and have found them to be very reliable and easy to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top