summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/TraversableLike.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-01-12 13:44:43 +0000
committerPaul Phillips <paulp@improving.org>2010-01-12 13:44:43 +0000
commita6a9f23ec16b153bb4f5c2c86fd79b85a1b06b91 (patch)
tree5f7978c6c5f37d327f01ddf2bc54d0463a8f1589 /src/library/scala/collection/TraversableLike.scala
parent38cfa95dd7241695b75bba8b964915887a5a8f6c (diff)
downloadscala-a6a9f23ec16b153bb4f5c2c86fd79b85a1b06b91.tar.gz
scala-a6a9f23ec16b153bb4f5c2c86fd79b85a1b06b91.tar.bz2
scala-a6a9f23ec16b153bb4f5c2c86fd79b85a1b06b91.zip
Added toMap to TraversableLike.
Diffstat (limited to 'src/library/scala/collection/TraversableLike.scala')
-rw-r--r--src/library/scala/collection/TraversableLike.scala20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala
index f570c4ca9a..a37341c2b5 100644
--- a/src/library/scala/collection/TraversableLike.scala
+++ b/src/library/scala/collection/TraversableLike.scala
@@ -984,9 +984,7 @@ self =>
def toList: List[A] = (new ListBuffer[A] ++= thisCollection).toList
/** Converts this $coll to an iterable collection.
- *
- * Note: Will not terminate for infinite-sized collections.
- *
+ * $willNotTerminateInf
* @return an `Iterable` containing all elements of this $coll.
*/
def toIterable: Iterable[A] = toStream
@@ -1015,6 +1013,22 @@ self =>
*/
def toSet[B >: A]: immutable.Set[B] = immutable.Set() ++ thisCollection
+ /** Converts this $coll to a map. This method is unavailable unless
+ * the elements are members of Tuple2, each ((K, V)) becoming a key-value
+ * pair in the map. Duplicate keys will be overwritten by later keys:
+ * if this is an unordered collection, which key is in the resulting map
+ * is undefined.
+ * $willNotTerminateInf
+ * @return a map containing all elements of this $coll.
+ */
+ def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = {
+ val b = immutable.Map.newBuilder[T, U]
+ for (x <- this)
+ b += x
+
+ b.result
+ }
+
/** Displays all elements of this $coll in a string using start, end, and separator strings.
*
* @param start the starting string.