11

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?.

Sagar Varpe
  • 3,531
  • 4
  • 27
  • 43

4 Answers4

19

You can choose between these:

So, yes, Scala has mutable lists :-)

Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
6

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

Graham
  • 651
  • 2
  • 10
  • 23
  • 1
    The accepted answer by Jean-Philippe Pellet is more direct, and gives you some classes that you can throw into your code just to get past your problem. However, Graham actually *answered* your question as to "why this is so?". I hope you'll read the link that he provided, as it will help you better understand the idea behind mutable and immutable collections in Scala. – Steve Perkins Jul 15 '11 at 12:45
3

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.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
0

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.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681