简体   繁体   中英

Change default capacity/load factor of scala mutable.HashMap

I am currently using scala 2.9.1. I create a mutable.HashMap using:

> val m = mutable.HashMap.empty[Int, Int]

I am kind of new to scala. In java, I was able to specify the capacity and load factor in the constructor of a HashMap. I am not able to find any way to do the same in scala.

Thanks in Advance

According to the API, this does not seem to be possible. One explanation is that mutable collections are strongly discouraged, and immutable collections don't need the default capacity information since the number of items must be known at the time of construction.

However, note that Scala will implicitly use default capacity information if you construct a collection (including mutable and immutable HashMap ) via the many available collection methods. For example, if you call map on a HashMap , it will use map defined on TraversableLike (reproduced below), and you can see that it provides a "size hint" to the builder that provides that capacity information.

def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
  val b = bf(repr)
  b.sizeHint(this)
  for (x <- this) b += f(x)
  b.result
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM