Create SortedMap from Iterator to scala

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 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:

 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.

+5
source share
2 answers

, , , , . , .to[NewColl]. , ,

import collection.immutable._

Iterator(1,2,3).to[SortedSet]

, - SortedMap varargs, :

SortedMap( List((1,"salmon"), (2,"herring")): _* )

( : _*, ). , Seq, Iterator.

, - , .

+5

. SortedMap.from API Scala 2.13.

0

All Articles