Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a bunch of different ways to accomplish this, only some of which have appeared in the answers thus far.</p> <p><strong>Method One:</strong> Since <code>java.util.HashMap</code> has the constructor <code>HashMap(Map&lt;? extends K,? extends V&gt; m)</code>, you could pass it a valid Java Map. And you can do this trivially with Scala's helpful <code>JavaConversions</code>:</p> <pre><code>scala&gt; import scala.collection.JavaConversions._ import scala.collection.JavaConversions._ scala&gt; val myMap = Map(1-&gt;"Hi",2-&gt;"Bye") myMap: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,Hi), (2,Bye)) scala&gt; val jmap = new java.util.HashMap[Int,String](myMap) // Need explicit types jmap: java.util.HashMap[Int,String] = {1=Hi, 2=Bye} </code></pre> <p>The downsides here are that you have to already have a Scala map (slightly wasteful if you're just going to create a Java one, perhaps), and that you have to specify the types. But it's compact and painless.</p> <p><strong>Method Two:</strong> Alternatively, you can create a new code block as the declaration statement, so you don't even need to have <code>JavaConversions</code> available:</p> <pre><code>scala&gt; val jmap2 = { | val x = new java.util.HashMap[Int,String] | for ((k,v) &lt;- List(1-&gt;"Howdy",2-&gt;"partner")) x.put(k,v) | x | } jmap2: java.util.HashMap[Int,String] = {1=Howdy, 2=partner} </code></pre> <p>Slightly less compact, but completely general, and as efficient (or inefficient) as you care to make it.</p> <p><strong>Method Three:</strong> Also, you can create an anonymous subclass of HashMap as long as it's okay to have a subclass (i.e. <code>.getClass</code> won't return <code>java.util.HashMap</code>), and use the initializer to set your values:</p> <pre><code>scala&gt; val jmap3 = new java.util.HashMap[Int,String] { | put(1,"Yo"); put(2,"bro") | } jmap3: java.util.HashMap[Int,String] = {1=Yo, 2=bro} scala&gt; jmap3.getClass.getName res0: java.lang.String = $anon$1 scala&gt; jmap3.getClass.getSuperclass.getName res1: java.lang.String = java.util.HashMap </code></pre> <p>The downside is, of course, that it's a subclass of <code>HashMap</code> rather than <code>HashMap</code>, but it's more compact than the assignment-from-code-block version since you don't need to assign the new map to a val.</p> <p><strong>Method Four:</strong> And finally, of course, you can create a method that does what you want and call it instead:</p> <pre><code>scala&gt; def newJHM[A,B](kv: Iterable[(A,B)]) = { | val jhm = new java.util.HashMap[A,B] | kv.foreach(i =&gt; jhm.put(i._1,i._2)) | jhm | } newJHM: [A,B](kv: Iterable[(A, B)])java.util.HashMap[A,B] scala&gt; val jmap4 = newJHM(Seq(1-&gt;"Bye",2-&gt;"Now")) // Type inference now works jmap4: java.util.HashMap[Int,java.lang.String] = {1=Bye, 2=Now} </code></pre> <p>This is barely less compact than the others and gets the types correct without you having to specify them, so it can be an appealing choice if you're doing this more than once.</p> <p>P.S. Just for fun, I've shown a variety of ways of getting some key-value pairs into the map, but they're not specific to a given method (except for #1 which requires a map). Mix and match at your preference.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload