Scala Tutorial - Learn How To Use Scala's Mutable ListBuffer
Overview
In this tutorial, we will learn how to use Scala's Mutable ListBuffer to perform common operations such as initialize a ListBuffer, access elements at specific index, add and remove elements and create an empty ListBuffer.
And, don't forget to review the Data Structures tutorial before delving into Scala's Immutable and Mutable collections.
More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Mutable ListBuffer will be discussed in Chapter 8 on Collection Functions.
What is a ListBuffer?
As per the Scala Documentation, a ListBuffer is resizable similar to an ArrayBuffer, except that it uses a Linked List as its internal data structure.
Steps
1. How to initialize a ListBuffer with 3 elements
The code below shows how to initialize a ListBuffer with 3 elements.
import scala.collection.mutable.ListBuffer
println("Step 1: How to initialize a ListBuffer with 3 elements")
val listBuffer1: ListBuffer[String] = ListBuffer("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of listBuffer1 = $listBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a ListBuffer with 3 elements
Elements of listBuffer1 = ListBuffer(Plain Donut, Strawberry Donut, Chocolate Donut)
2. How to access elements at specific index in a ListBuffer
The code below shows how to access elements at specific index in a ListBuffer.
println("\nStep 2: How to access elements at specific index in a ListBuffer")
println(s"Element at index 0 = ${listBuffer1(0)}")
println(s"Element at index 1 = ${listBuffer1(1)}")
println(s"Element at index 2 = ${listBuffer1(2)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to access elements at specific index in a ListBuffer
Element at index 0 = Plain Donut
Element at index 1 = Strawberry Donut
Element at index 2 = Chocolate Donut
3. How to add elements to a ListBuffer using +=
The code below shows how to add elements to a ListBuffer using +=.
println("\nStep 3: How to add elements to a ListBuffer using +=")
listBuffer1 += "Vanilla Donut"
println(s"Elements of listBuffer1 = $listBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to add elements to a ListBuffer using +=
Elements of listBuffer1 = ListBuffer(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)
4. How to add elements from a List to a ListBuffer using ++=
The code below shows how to add elements from a List to a ListBuffer using ++=.
println("\nStep 4: How to add elements from a List to a ListBuffer using ++=")
listBuffer1 ++= List[String]("Glazed Donut", "Krispy creme")
println(s"Elements of listBuffer1 = $listBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to add elements from a List to a ListBuffer using ++=
Elements of listBuffer1 = ListBuffer(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut, Glazed Donut, Krispy creme)
5. How to remove elements from a ListBuffer
The code below shows how to remove elements from a ListBuffer.
println("\nStep 5: How to remove elements from a ListBuffer")
listBuffer1 -= "Plain Donut"
println(s"Elements of listBuffer1 = $listBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to remove elements from a ListBuffer
Elements of listBuffer1 = ListBuffer(Strawberry Donut, Chocolate Donut, Vanilla Donut, Glazed Donut, Krispy creme)
6. How to remove elements from a List to a ListBuffer using --=
The code below shows how to remove elements from a List to a ListBuffer using --=.
println("\nStep 6: How to remove elements from a List to a ListBuffer using --=")
listBuffer1 --= List[String]("Glazed Donut", "Krispy creme")
println(s"Elements of listBuffer1 = $listBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 6: How to remove elements from a List to a ListBuffer using --=
Elements of listBuffer1 = ListBuffer(Strawberry Donut, Chocolate Donut, Vanilla Donut)
7. How to initialize an empty ListBuffer
The code below shows how to initialize an empty ListBuffer.
println("\nStep 7: How to initialize an empty ListBuffer")
val emptyListBuffer: ListBuffer[String] = ListBuffer.empty[String]
println(s"Empty list buffer = $emptyListBuffer")
You should see the following output when you run your Scala application in IntelliJ:
Step 7: How to initialize an empty ListBuffer
Empty list buffer = ListBuffer()
Summary
In this tutorial, we went over the following:
- How to initialize a ListBuffer
- How to access elements at specific index in a ListBuffer
- How to add elements to a ListBuffer using +=
- How to add elements from a List to a ListBuffer using ++=
- How to remove elements from a ListBuffer
- How to remove elements from a List to a ListBuffer using --=
- How to initialize an empty ListBuffer
Tip
- Don't forget to review Chapter 8 on Collection Functions where we will go over the rich suite of functions exposed on collections such as aggregate, collect, fold, reduce, map, flatMap, scan, partition etc
- Scala's documentation on mutable ListBuffer.
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to use Scala's Mutable Map.