Sagar Varpe
Sagar Varpe

Reputation: 3599

No Scala mutable list

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

Answers (4)

Daniel C. Sobral
Daniel C. Sobral

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

Alexey Romanov
Alexey Romanov

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

Graham
Graham

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

Related Questions