I have a java.util.Treemap[Long, Int] in Scala. What I need to do is convert the values in the Treemap into an Array[Int].
I can either get an Iterator[Int] or Collection[Int] from the TreeMap, but I’m not sure how to convert these into an Array[Int].
Any suggestions would be appreciated.
The problem with
t.values.toArrayis thatjava.util.Collectionalready has atoArray()method, which is why it returns anInteger[]which looks like anArray[AnyRef]to Scala. If Java didn’t have such a method, the implicit inJavaConversionswould kick in and covert it to to anIterable[Int]before building the Array. So you can either call the coversion explicitly or give a type hint:or alternatively
On my machine these are
over twiceabout 30 times as fast as usingtoMap, for a 1000-entry TreeMap.