Possible Duplicate:
Scala list concatenation, ::: vs ++
In Scala, say I have two lists
scala> val oneTwo = List(1,2)
oneTwo: List[Int] = List(1, 2)
and
scala> val threeFour = List(3,4)
threeFour: List[Int] = List(3, 4)
I can concatenates Lists by doing:
scala> oneTwo ::: threeFour
res30: List[Int] = List(1, 2, 3, 4)
Or
scala> oneTwo ++ threeFour
res31: List[Int] = List(1, 2, 3, 4)
What is the difference between both approaches?
Thanks.
The
:::method is specific toList, while++is part of anyTraversable.The difference arise out of two things. First,
Listis one of the original Scala collections, used a lot in the compiler, and subject to special optimizations. The::concatenation is the same as used in the ML family of languages, one of the big Scala inspirations, and:::extrapolates from it.On the other hand,
++came along with the redesign of Scala collections on Scala 2.8.0, which made methods and inheritance uniform. I think it existed before that (onSet, for example), but the collections did not share a common superclass, so it was basically an ad hoc method for other collections.In terms of performance,
:::should beat++, but probably not significantly.