Skip to content

Commit 0725809

Browse files
committed
Use a LazyKeySet for LinkedHashMap's keySet
LinkedHashMap's `keySet` has always been returning a LinkedKeySet, a private data structure that was extended from the generic MapOps.KeySet. Recently MapOps.KeySet was changed to a strict data structure, so we change the `keySet` method to use the lazy wrapper instead. Unfortunately we cannot change the hierarchy of `LinkedKeySet`, but the API never said we have to use it.
1 parent 9c7cd89 commit 0725809

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

library/src/scala/collection/Map.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ object MapOps {
426426

427427
/** The implementation class of the set returned by `keySet`, for pure maps.
428428
*/
429-
private class LazyKeySet[K, +V, +CC[_, _] <: IterableOps[?, AnyConstr, ?], +C](mp: MapOps[K, V, CC, C]) extends AbstractSet[K] with DefaultSerializable {
429+
private[collection] class LazyKeySet[K, +V, +CC[_, _] <: IterableOps[?, AnyConstr, ?], +C](mp: MapOps[K, V, CC, C]) extends AbstractSet[K] with DefaultSerializable {
430430
def iterator: Iterator[K] = mp.keysIterator
431431
def diff(that: Set[K]): Set[K] = LazyKeySet.this.fromSpecific(this.view.filterNot(that))
432432
def contains(key: K): Boolean = mp.contains(key)

library/src/scala/collection/mutable/LinkedHashMap.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,14 @@ class LinkedHashMap[K, V]
234234
def extract(nd: Entry): (K, V) = (nd.key, nd.value)
235235
}
236236

237+
/** Note that a LinkedKeySet could be strict. */
237238
protected class LinkedKeySet extends KeySet {
238239
override def iterableFactory: IterableFactory[collection.Set] = LinkedHashSet
239240
}
240241

241-
override def keySet: collection.Set[K] = new LinkedKeySet
242+
override def keySet: collection.Set[K] = new MapOps.LazyKeySet(this) {
243+
override def iterableFactory: IterableFactory[collection.Set] = LinkedHashSet
244+
}
242245

243246
override def keysIterator: Iterator[K] =
244247
if (size == 0) Iterator.empty

0 commit comments

Comments
 (0)