Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mutable collections in Scala

Tags:

I think I may be failing to understand how mutable collections work. I would expect mutable collections to be affected by applying map to them or adding new elements, however:

scala> val s: collection.mutable.Seq[Int] = collection.mutable.Seq(1)
s: scala.collection.mutable.Seq[Int] = ArrayBuffer(1)

scala> s :+ 2 //appended an element
res32: scala.collection.mutable.Seq[Int] = ArrayBuffer(1, 2)

scala> s //the original collection is unchanged
res33: scala.collection.mutable.Seq[Int] = ArrayBuffer(1)

scala> s.map(_.toString) //mapped a function to it
res34: scala.collection.mutable.Seq[java.lang.String] = ArrayBuffer(1)

scala> s //original is unchanged
res35: scala.collection.mutable.Seq[Int] = ArrayBuffer(1)

//maybe mapping a function that changes the type of the collection shouldn't work
//try Int => Int

scala> s.map(_ + 1)
res36: scala.collection.mutable.Seq[Int] = ArrayBuffer(2)

scala> s //original unchanged
res37: scala.collection.mutable.Seq[Int] = ArrayBuffer(1)

This behaviour doesn't seem to be separate from the immutable collections, so when do they behave separately?

like image 990
Henry Henrinson Avatar asked Aug 02 '11 09:08

Henry Henrinson


People also ask

What is mutable collection in Scala?

Scala collections systematically distinguish between mutable and immutable collections. A mutable collection can be updated or extended in place. This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change.

How do I create a mutable list in Scala?

Because a List is immutable, if you need to create a list that is constantly changing, the preferred approach is to use a ListBuffer while the list is being modified, then convert it to a List when a List is needed. The ListBuffer Scaladoc states that a ListBuffer is “a Buffer implementation backed by a list.

How can we convert mutable map to immutable Scala?

If you just want a mutable HashMap , you can just use x. toMap in 2.8 or collection. immutable. Map(x.

What are different collections in Scala?

There are two types of collections in Scala – mutable and immutable.


2 Answers

For both immutable and mutable collections, :+ and +: create new collections. If you want mutable collections that automatically grow, use the += and +=: methods defined by collection.mutable.Buffer.

Similarly, map returns a new collection — look for transform to change the collection in place.

like image 174
Jean-Philippe Pellet Avatar answered Oct 11 '22 01:10

Jean-Philippe Pellet


map operation applies the given function to all the elements of collection, and produces a new collection.

The operation you are looking for is called transform. You can think of it as an in-place map except that the transformation function has to be of type a -> a instead of a -> b.

scala> import collection.mutable.Buffer
import collection.mutable.Buffer

scala> Buffer(6, 3, 90)
res1: scala.collection.mutable.Buffer[Int] = ArrayBuffer(6, 3, 90)

scala> res1 transform { 2 * }
res2: res1.type = ArrayBuffer(12, 6, 180)

scala> res1
res3: scala.collection.mutable.Buffer[Int] = ArrayBuffer(12, 6, 180)
like image 26
missingfaktor Avatar answered Oct 10 '22 23:10

missingfaktor