Specifying the size of a HashMap in Scala

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

Tags:

Hashmap

Scala