Reputation: 864
I'm using Scala's mutable HashMap to incrementally add up to millions of key-value pairs. The resizing of these HashMaps is now the slowest part of my program. How can I tell Scala to create a very large HashMap from the start so that it (almost) never needs to be resized?
I would appreciate also any ideas that propose another Scala/Java collection that would suit my needs. Adding a new key-value pair and retrieving a value given a key should both be doable in approximately constant time.
Upvotes: 12
Views: 3843
Reputation: 24637
java.util.HashMap
has a constructor that lets you specify an initial size. Unlike Yuriy's answer, there's no reason to write code that you don't need to.
jifeng.yin's answer is also acceptable, although since OpenHashMap
uses an unspecified form of Open Addressing, you should benchmark for your use case and see how the change from chaining to open addressing matters, if it all.
Upvotes: 0
Reputation: 2769
One of possible way:
import scala.collection.mutable.{HashTable, DefaultEntry}
trait BigHashTable[A, B] extends HashTable[A, DefaultEntry[A, B]] {
override def initialSize: Int = 1024 // 16 - by default
}
val x = new HashMap[Int, String] with BigHashTable[Int, String]
another one:
class MyHashMap[A, B](initSize : Int) extends HashMap[A, B] {
override def initialSize: Int = initSize // 16 - by default
}
val x = new MyHashMap[Int, String](1024)
Upvotes: 11