diff options
author | Rex Kerr <ichoran@gmail.com> | 2014-11-24 17:30:35 -0800 |
---|---|---|
committer | Rex Kerr <ichoran@gmail.com> | 2014-11-25 11:41:40 -0800 |
commit | 1f8e81d8993aed7f5b1635384a0a48e265d0dcd8 (patch) | |
tree | 5e353d0c352e3d65954a541bf53ec9ed6002e455 /src/library/scala/collection/MapLike.scala | |
parent | 6f5b853dda0b00c65776b05abde23ceb03d9621d (diff) | |
download | scala-1f8e81d8993aed7f5b1635384a0a48e265d0dcd8.tar.gz scala-1f8e81d8993aed7f5b1635384a0a48e265d0dcd8.tar.bz2 scala-1f8e81d8993aed7f5b1635384a0a48e265d0dcd8.zip |
SI-8727 Map.filterKeys result's contains and get are inconsistent
Changed documentation and code so that predicate for filterKeys is always applied before the map is queried.
Diffstat (limited to 'src/library/scala/collection/MapLike.scala')
-rw-r--r-- | src/library/scala/collection/MapLike.scala | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/src/library/scala/collection/MapLike.scala b/src/library/scala/collection/MapLike.scala index 5ec7d5c615..3de30c2c8b 100644 --- a/src/library/scala/collection/MapLike.scala +++ b/src/library/scala/collection/MapLike.scala @@ -230,11 +230,15 @@ self => protected class FilteredKeys(p: A => Boolean) extends AbstractMap[A, B] with DefaultMap[A, B] { override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv) def iterator = self.iterator.filter(kv => p(kv._1)) - override def contains(key: A) = self.contains(key) && p(key) + override def contains(key: A) = p(key) && self.contains(key) def get(key: A) = if (!p(key)) None else self.get(key) } /** Filters this map by retaining only keys satisfying a predicate. + * + * '''Note''': the predicate must accept any key of type `A`, not just those already + * present in the map, as the predicate is tested before the underlying map is queried. + * * @param p the predicate used to test keys * @return an immutable map consisting only of those key value pairs of this map where the key satisfies * the predicate `p`. The resulting map wraps the original map without copying any elements. |