How to populate java.util.HashMap on the fly from Scala code?
问题 I am unit testing java code from ScalaTest and would like to populate a java.util.HashMap within the same statement it gets declared. Is it possible to do this in Scala? 回答1: There are a bunch of different ways to accomplish this, only some of which have appeared in the answers thus far. Method One: Since java.util.HashMap has the constructor HashMap(Map<? extends K,? extends V> m) , you could pass it a valid Java Map. And you can do this trivially with Scala's helpful JavaConversions : scala