I'm pretty new to Scala and try to understand mutable Seq
. Since it's in package mutable
I expected there is a method that allows us to append element without copying the whole collection.
But there is no +=
method in the mutable.Seq
, but in Buffer
is. :+
and +:
both copy the collection.
So why is it mutable?
mutable. Seq has update , that allows you to change the element at a given index, but it does not grow or shrink. Buffer is s specialization of Seq , that is both mutable and growable.
Scala's default Seq class is mutable. Yes, you read that right.
Scala Seq is a trait to represent immutable sequences. This structure provides index based access and various utility methods to find elements, their occurences and subsequences. A Seq maintains the insertion order.
Because mutable
and growable
isn't the same thing.
(the latter is one specific type of the former: everything, that's growable is mutable, but not everything that's mutable is growable).
mutable.Seq
has update
, that allows you to change the element at a given index, but it does not grow or shrink.
Buffer
is s specialization of Seq
, that is both mutable and growable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With