Reputation: 3599
Scala has both a mutable and an immutable Map , but it has only an immutable List. If you want a mutable List you need a ListBuffer.
I don't understand why this is so. Any one knows?.
Upvotes: 11
Views: 4972
Reputation: 297275
Map
is a trait
-- like Java's interface
--, while List
is a class
, a concrete implementation of a Seq
. There are mutable and immutable Seq
, just like for Map
.
This may be confusing to Java programmers because, in Java, List
is an interface
, whose (main) implementations are ArrayList
and LinkedList
. Alas, Java naming is atrocious. First, ArrayList
is not a List
by any stretch of imagination. Also, the interface has methods that are not really related to any traditional list.
So, if you want mutable/immutable equivalence, look to concrete subclass implementations of Seq
.
Upvotes: 0
Reputation: 60006
You can choose between these:
scala.collection.mutable.DoubleLinkedList
scala.collection.mutable.LinkedList
scala.collection.mutable.ListBuffer
scala.collection.mutable.MutableList
So, yes, Scala has mutable lists :-)
Upvotes: 19
Reputation: 170899
There is a mutable List
, but it is called Buffer
. The article linked by Graham goes into more depth, but I thought there should be a specific answer to the question as well.
Upvotes: 3
Reputation: 661
I hope that this article may be of some use to you. The diagram at the bottom of the page is particularly useful in providing the mutable and immutable classes.
http://www.scala-lang.org/docu/files/collections-api/collections_1.html
Upvotes: 6