Skip to content

Commit 07e5274

Browse files
authored
Merge pull request scala/scala#6828 from retronym/ticket/10703
Several entries with the same key in mutable.HashMap
2 parents c32125b + 0b48bb6 commit 07e5274

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

library/src/scala/collection/mutable/HashMap.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,25 @@ extends AbstractMap[A, B]
7575
override def getOrElseUpdate(key: A, defaultValue: => B): B = {
7676
val hash = elemHashCode(key)
7777
val i = index(hash)
78-
val entry = findEntry(key, i)
79-
if (entry != null) entry.value
78+
val firstEntry = findEntry(key, i)
79+
if (firstEntry != null) firstEntry.value
8080
else {
8181
val table0 = table
8282
val default = defaultValue
8383
// Avoid recomputing index if the `defaultValue()` hasn't triggered
8484
// a table resize.
8585
val newEntryIndex = if (table0 eq table) i else index(hash)
86-
addEntry(createNewEntry(key, default), newEntryIndex)
86+
val e = createNewEntry(key, default)
87+
// Repeat search
88+
// because evaluation of `default` can bring entry with `key`
89+
val secondEntry = findEntry(key, newEntryIndex)
90+
if (secondEntry == null) addEntry0(e, newEntryIndex)
91+
else secondEntry.value = default
92+
default
8793
}
8894
}
8995

96+
9097
/* inlined HashTable.findEntry0 to preserve its visibility */
9198
private[this] def findEntry(key: A, h: Int): Entry = {
9299
var e = table(h).asInstanceOf[Entry]

0 commit comments

Comments
 (0)