There is a real difference between the two due to where these methods are defined:
The apply
constructor is defined in the companion of the target collection:
object Coll { def apply[T](els: T*): Coll[T] = ??? }
This means that it has absolutely no input information other than Seq
elements, so it just goes through this sequence and builds Coll
.
on the other hand, toSeq
, toArray
, ... are defined in the original collection, that is, they have all the additional information about its internal components.
You can usually rely on toXXX
methods to short-circuit when necessary, or to return some specialized collection that makes sense.
As a stupid example, consider:
List(1,2,3).toSeq // toSeq returns `this`, there is no overhead // vs. Seq(List(1,2,3): _*) // this will copy that list into a new one...
It gets worse if you replace this list with Stream.continually(1)
...
source share