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 ?
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.
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.
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 anArrayDeque
instead, or aList
and avar
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With