Scala Tutorial - Learn How To Use Scala's Mutable ListBuffer

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:44 pm

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.

 

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()

This concludes our tutorial on Learn How To Use Scala's Mutable ListBuffer and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

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

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.

 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on