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.
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)
scala.collection.mutable.OpenHashMap supports initialSize
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