diff options
-rw-r--r-- | src/library/scala/collection/TraversableLike.scala | 20 |
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. |