斯卡拉2.11 mutable.Map
示例化太多 DefaultEntry
.
要复制的代码:
val map: mutable.Map[Int, String] = mutable.Map()
(1 to 1000000).foreach(n => map(n.toString) = s"number $n")
以下是visualvm的内存示例:
我没想到会看到这么多 DefaultEntry
. 原因是什么?
斯卡拉2.11 mutable.Map
示例化太多 DefaultEntry
.
要复制的代码:
val map: mutable.Map[Int, String] = mutable.Map()
(1 to 1000000).foreach(n => map(n.toString) = s"number $n")
以下是visualvm的内存示例:
我没想到会看到这么多 DefaultEntry
. 原因是什么?
1条答案
按热度按时间l2osamch1#
您的代码是正确的,示例也是正确的。给你1000欧元
n
对每一个n
你叫:override def update(key: A, value: B): Unit = put(key, value)
override def put(key: A, value: B): Option[B] = {
val e = findOrAddEntry(key, value)
if (e eq null) None
else { val v = e.value; e.value = value; Some(v) }
}
protected def findOrAddEntry[B](key: A, value: B): Entry = {
val h = index(elemHashCode(key))
val e = findEntry0(key, h)
if (e ne null) e else { addEntry0(createNewEntry(key, value), h); null }
}
protected def createNewEntry[B1](key: A, value: B1): Entry = {
new Entry(key, value.asInstanceOf[B])
}