I have val it:Iterator[(A,B)]one and I want to create SortedMap[A,B]with elements that I get from Iterator. Now I do it like this:
val it:Iterator[(A,B)]
SortedMap[A,B]
Iterator
val map = SortedMap[A,B]() ++ it
It works great, but it seems a bit inconvenient to use. I checked the document SortedMap, but could not find anything more elegant. There is something like:
SortedMap
it.toSortedMap
or
SortedMap.from(it)
in the Scala standard library, which maybe I missed?
Edit : mixing both ideas with @Rex answer. I came up with the following:
SortedMap(it.to:_*)
Which works just fine and does not allow you to specify a type signature SortedMap. Still looks funny, so further answers are welcome.
, , , , . , .to[NewColl]. , ,
.to[NewColl]
import collection.immutable._ Iterator(1,2,3).to[SortedSet]
, - SortedMap varargs, :
SortedMap( List((1,"salmon"), (2,"herring")): _* )
( : _*, ). , Seq, Iterator.
: _*
Seq
, - , .
. SortedMap.from API Scala 2.13.
SortedMap.from