Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In this tutorial, we’ll dig into the Scala ArrayBuffer class, which is one of the many available collections in the standard library.
Scala provides a wide range of different collections like List, Set, Map, and many others not so commonly used. The Scala ArrayBuffer is one of those.
There are two main groups of collections in Scala: immutable and mutable collections. While the Scala community is biased towards the usage of immutable data structures, the standard lib also provides many mutable collections. One such example is the ArrayBuffer class which is very similar to normal Java Lists.
An ArrayBuffer is a specific implementation of a mutable list. This means that we can keep adding more elements to the list after the creation moment. If we exceed the current max size of the list, then it will allocate another bigger in-memory array and copy all the elements there. For immutable lists, a new list is always created if we try to append another element.
Another important detail is that the ArrayBuffer implementation in specific is backed up by an indexed array, meaning that we have constant access to any element of the collection.
Creating a new instance is very straightforward:
scala> import scala.collection.mutable.ArrayBuffer
scala> val nums = ArrayBuffer(1, 2, 3)
val nums: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
And we have our new instance.
To add an element, there are a few different solutions:
scala> import scala.collection.mutable.ArrayBuffer
scala> val l = ArrayBuffer(1, 2, 3)
val l: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> l += 4
val res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
scala> l.append(5)
val res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> l.prepend(0)
val res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5)
scala> l.insert(0, -1)
scala> l
val res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 1, 2, 3, 4, 5)
ArrayBuffer allows us to insert a new element both at the beginning and end of the list. Furthermore, since it’s an indexed list, it’s also easy to insert an element in any position of the list.
To remove elements, there are also a few different methods available:
scala> import scala.collection.mutable.ArrayBuffer
scala> val l = ArrayBuffer(1, 2, 3, 4, 5, 6, 7)
val l: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6, 7)
scala> l -= 3
val res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 4, 5, 6, 7)
scala> l.remove(3,2) // starting from index 3, remove 2 elements
scala> l
val res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 4, 7)
Similar to the insertion, we can also remove elements from any position of the list.
If we need to concatenate another List or any other collection, we can use the ArrayBuffer#appendAll method:
scala> val l = ArrayBuffer(1, 2, 3)
val l: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> l.appendAll(List(4,5,6))
val res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6)
In this article, we saw what is the ArrayBuffer class and some of the more common operations.