A Scala Iterable has a toMap
method, which returns a Map. What is this map backed by? What are its performance characteristics?
Is there any way to specify that toMap
should return a HashMap
?
It returns an immutable.HashMap
, which is actually an immutable hash array mapped trie. This data structure is essentially a hybrid between a multilevel hashtable and a trie. The worst-case complexity of a hash array mapped trie is O(log n)
for all operations, although with a very low constant factor - hash array mapped tries are very shallow, and typically have only a few indirections. You can read more about the performance characteristics here or run a couple of microbencharks. The performance is acceptable in most cases.
The toMap
always returns a hash trie. If you want a mutable hash table, then do this:
import collection._
mutable.HashMap() ++= xs
instead of:
xs.toMap
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With