26 Mutable Collections

Scala “encourages” using immutable collections (hence they are those used by default) however sometimes mutable collections might have some benefits, either for CPU / memory performance, for code readability or simply a matter of preference. As we saw earlier, Scala provides concrete mutable collections in scala.collections.mutable

  • Best practice suggests that you prefix mutable collections with mutable, this might not seem useful for ArrayBuffer, but it will for scala.collections.mutable.Map
  • ArrayBuffer is the more or less equivalent to java’s java.util.ArrayList which is backed by an array
  • ListBuffer is the mutable partner for scala’s immutable List, implemented as a Linked List
  • There are many other mutable collections types, see links below for more information

See Also opens in new page

Code editor is using Scalakata.com written by Guillaume Massé