summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/Map.scala
diff options
context:
space:
mode:
authorLex Spoon <lex@lexspoon.org>2007-03-23 16:46:49 +0000
committerLex Spoon <lex@lexspoon.org>2007-03-23 16:46:49 +0000
commit96fd08897359e2851aad31b7b515225dd02b0195 (patch)
tree9e4f726af790c7599bf21bb9909f36e00ea41a8f /src/library/scala/collection/Map.scala
parent16a3288cce270b3a8b03a85e2f3a0fb321c125ee (diff)
downloadscala-96fd08897359e2851aad31b7b515225dd02b0195.tar.gz
scala-96fd08897359e2851aad31b7b515225dd02b0195.tar.bz2
scala-96fd08897359e2851aad31b7b515225dd02b0195.zip
- toString() on large collections now only prints
a few of the elements - all maps now have getOrElse - mutable maps now have getOrElsePut
Diffstat (limited to 'src/library/scala/collection/Map.scala')
-rw-r--r--src/library/scala/collection/Map.scala25
1 files changed, 22 insertions, 3 deletions
diff --git a/src/library/scala/collection/Map.scala b/src/library/scala/collection/Map.scala
index 6b7e7cd0b1..d10b063d21 100644
--- a/src/library/scala/collection/Map.scala
+++ b/src/library/scala/collection/Map.scala
@@ -50,6 +50,15 @@ trait Map[A, +B] extends PartialFunction[A, B] with Iterable[(A, B)] {
*/
def get(key: A): Option[B]
+ /** Check if this map maps <code>key</code> to a value.
+ * Return that value if it exists, otherwise return <code>default</code>.
+ */
+ def getOrElse[B2 >: B](key: A, default: B2): B2 =
+ get(key) match {
+ case Some(v) => v
+ case None => default
+ }
+
/** Is this an empty map?
*
* @return <code>true</code> iff the map is empty.
@@ -139,10 +148,20 @@ trait Map[A, +B] extends PartialFunction[A, B] with Iterable[(A, B)] {
/** Creates a string representation for this map.
*
- * @return a string showing all mappings
+ * @return a string showing all mappings, or a subset of them
+ * if the map is large.
*/
- override def toString() =
- elements.toList.map(kv => kv._1 + " -> " + kv._2).mkString("Map(", ", ", ")")
+ override def toString() = {
+ def elem2str(kv: (A, B)) = kv._1 + " -> " + kv._2
+ if(size <= 20)
+ elements.map(elem2str ).mkString("Map(", ", ", ")")
+ else {
+ val topr = 2
+ val initStrs = elements.take(topr).map(elem2str)
+ initStrs.mkString("Map(", ", ",
+ ", and " + (size - topr) + " more ...)")
+ }
+ }
/** The default value for the map, returned when a key is not found
* The method implemented here yields an error,