siaw23 siaw23 - 1 year ago 314
Scala Question

Add element to a list In Scala

I'm running Scala 2.10.2. I want to create a list, then add some elements to the list and expect to see all the elements in the lists when I call the list's name. But I observed something quite weird (At least weird for me since I'm a newbie). Below is the what I tried to do in my

sbt console

scala> val l = 1.0 :: 5.5 :: Nil
l: List[Double] = List(1.0, 5.5)

scala> l
res0: List[Double] = List(1.0, 5.5)

scala> l ::: List(2.2, 3.7)
res1: List[Double] = List(1.0, 5.5, 2.2, 3.7)

scala> List(l) :+ 2.2
res2: List[Any] = List(List(1.0, 5.5), 2.2)

scala> l
res3: List[Double] = List(1.0, 5.5)


First, I created the list
with 2 elements (1.0 and 5.5). I call
and get what I expect; the two elements. Now I tried to add another element to the list using
which returned a new list with a new list of elements I added (2.2 and 3.7) Sweet! I even checked someone else's code for help: Appending an element to the end of a list in Scala to use a new construct
. So at this stage I'm all happy, but I call
and I get the unexpected: `res3: List[Double] = List(1.0, 5.5)'.

Where are the elements I added? And how do I add these elements correctly so that when I call
I get a new list with all the stuff I added?

Answer Source

You are using an immutable list. The operations on the List return a new List. The old List remains unchanged. This can be very useful if another class / method holds a reference to the original collection and is relying on it remaining unchanged. You can either use different named vals as in

val myList1 = 1.0 :: 5.5 :: Nil 
val myList2 = 2.2 :: 3.7 :: mylist1

or use a var as in

var myList = 1.0 :: 5.5 :: Nil 
myList :::= List(2.2, 3.7)

This is equivalent syntax for:

myList = myList.:::(List(2.2, 3.7))

Or you could use one of the mutable collections such as

val myList = scala.collection.mutable.MutableList(1.0, 5.5)
myList.++=(List(2.2, 3.7))

Not to be confused with the following that does not modify the original mutable List, but returns a new value:

myList.++:(List(2.2, 3.7))

However you should only use mutable collections in performance critical code. Immutable collections are much easier to reason about and use. One big advantage is that immutable List and scala.collection.immutable.Vector are Covariant. Don't worry if that doesn't mean anything to you yet. The advantage of it is you can use it without fully understanding it. Hence the collection you were using by default is actually scala.collection.immutable.List its just imported for you automatically.

Note: personally I use Seq as my default collection. This uses the immutable Seq trait. List /Vector / Array is an implementation detail for performance not functionality. List is more efficient for small collections.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download