Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala 2.13 what to use instead of MutableList?

I'm upgrading a software from Scala 2.12.8 to Scala 2.13, and figure out that the collection MutableList (scala.collection.mutable.MutableList) was removed according to many guides (like this one).

This guide, for example, say that this was a deprecated collection so that's why it was removed, but I cannot find any deprecation in that class in previous versions.

"Deprecated collections were removed (MutableList, immutable.Stack, others)"

I also upgrade first to 2.12.9 (latest before 2.13.0) to check it there was any deprecated annotation giving a suggestion on what to use instead, but also in this version the collection is not deprecated.

I searched for this question I couldn't find a good answer. This question is gonna be good for me and also for future upgrades.

What should I use instead of MutableList, in Scala 2.13 ?

like image 333
Fernando Rezk Avatar asked Aug 13 '19 09:08

Fernando Rezk


People also ask

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.

Is sequence mutable Scala?

Scala's default Seq class is mutable. Yes, you read that right. I think it's an extremely important thing to be aware of, and I'm not sure it's known widely enough.


1 Answers

According to https://docs.scala-lang.org/overviews/core/collections-migration-213.html:

collection.mutable.MutableList was not deprecated in 2.12 but was considered to be an implementation detail for implementing other collections. Use an ArrayDeque instead, or a List and a var.

scala> val dq = new ArrayDeque[Int]
dq: scala.collection.mutable.ArrayDeque[Int] = ArrayDeque()

scala> dq.append(1)
res1: dq.type = ArrayDeque(1)

scala> dq.append(2)
res2: dq.type = ArrayDeque(1, 2)

scala> dq
res3: scala.collection.mutable.ArrayDeque[Int] = ArrayDeque(1, 2)
like image 191
Yuval Itzchakov Avatar answered Sep 18 '22 12:09

Yuval Itzchakov