St.Antario
St.Antario

Reputation: 27375

Scala to Java conversions

I want to use a Map with Object's reference == comparison in Scala. In Java we have java.util.IdentityHashMap so I would like to use it. I did this:

  val channels: Map[String, Handler] = (new util.IdentityHashMap).asScala.toMap

The issue is the docuemntation only says that

Converts a Java collection to the corresponding Scala collection

So it's not clear if it's just a view of the Java collection or a newly created scala.collection?

Upvotes: 0

Views: 100

Answers (1)

thwiegan
thwiegan

Reputation: 2173

If you take a look at the implicit definitions for the specific types, you can see mapAsScalaMapConverter is documented as:

Adds an asScala method that implicitly converts a Java Map to a Scala mutable Map. The returned Scala Map is backed by the provided Java Map and any side-effects of using it via the Scala interface will be visible via the Java interface and vice versa.

If the Java Map was previously obtained from an implicit or explicit call of asMap(scala.collection.mutable.Map) then the original Scala Map will be returned.

The bold printed part should answer your question.

Upvotes: 4

Related Questions