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

By Nadim Bahadoor | Last updated: February 3, 2020 at 7:30 am

Overview

In this tutorial, we will learn how to use Scala's Mutable ListMap to perform common operations such as initialize a ListMap, access elements by key, add and remove elements and create an empty ListMap.

 

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 ListMap will be discussed in Chapter 8 on Collection Functions.

 

What is a ListMap?

As per the Scala Documentation, a ListMap is a collection of key and value pairs where the keys are backed by a List data structure.

 

Steps

1. How to initialize a ListMap with 3 elements

The code below shows how to initialize a ListMap with 3 elements.


import scala.collection.mutable.ListMap
println("\nStep 1: How to initialize a ListMap with 3 elements")
val listMap1: ListMap[String, String] = ListMap("PD" -> "Plain Donut", "SD" ->"Strawberry Donut", "CD" -> "Chocolate Donut")
println(s"Elements of listMap1 = $listMap1")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize a ListMap with 3 elements
Elements of listMap1 = Map(CD -> Chocolate Donut, PD -> Plain Donut, SD -> Strawberry Donut)

 

2. How to initialize ListMap using key -> value notation

The code below shows how to initialize ListMap using key -> value notation.


println("\nStep 2: How to initialize ListMap using key -> value notation")
val listMap2: ListMap[String, String] = ListMap("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut")
println(s"Elements of listMap1 = $listMap2")

You should see the following output when you run your Scala application in IntelliJ:

Step 2: How to initialize ListMap using key -> value notation
Elements of listMap1 = Map(GD -> Glazed Donut, VD -> Vanilla Donut)

3. How to access elements of ListMap by specific key

The code below shows how to access elements of ListMap by specific key.


println("\nStep 3: How to access elements of ListMap by specific key")
println(s"Element by key VD = ${listMap2("VD")}")
println(s"Element by key GD = ${listMap2("GD")}")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to access elements of ListMap by specific key
Element by key VD = Vanilla Donut
Element by key GD = Glazed Donut

 

4. How to add elements to ListMap using +=

The code below shows how to add elements to ListMap using +=.


println("\nStep 4: How to add elements to ListMap using +")
listMap1 += ("KD" -> "Krispy Kreme Donut")
println(s"Element of listMap1 = ${listMap1}")

You should see the following output when you run your Scala application in IntelliJ:

Step 4: How to add elements to ListMap using +=
Element of listMap1 = Map(KD -> Krispy Kreme Donut, SD -> Strawberry Donut, PD -> Plain Donut, CD -> Chocolate Donut)

 

5. How to add elements from a ListMap to an existing ListMap using ++=

The code below shows how to add elements from a ListMap to an existing ListMap using ++=.

println("\nStep 5: How to add elements from a ListMap to an existing ListMap using ++=")
listMap1 ++= listMap2
println(s"Element of listMap1 = ${listMap1}")

You should see the following output when you run your Scala application in IntelliJ:

Step 5: How to add elements from a ListMap to an existing ListMap using ++=
Element of listMap1 = Map(VD -> Vanilla Donut, KD -> Krispy Kreme Donut, SD -> Strawberry Donut, PD -> Plain Donut, CD -> Chocolate Donut, GD -> Glazed Donut)

 

6. How to remove key and its value from ListMap using -=

The code below shows how to remove key and its value from ListMap using -=.


println("\nStep 6: How to remove key and its value from ListMap using -=")
listMap1 -= ("CD")
println(s"ListMap without the key CD and its value = $listMap1")

You should see the following output when you run your Scala application in IntelliJ:


Step 6: How to remove key and its value from ListMap using -=
ListMap without the key CD and its value = Map(PD -> Plain Donut, SD -> Strawberry Donut, KD -> Krispy Kreme Donut, VD -> Vanilla Donut, GD -> Glazed Donut)

 

7. How to initialize an empty ListMap

The code below shows how to initialize an empty ListMap.


println("\nStep 7: How to initialize an empty ListMap")
val emptyListMap: ListMap[String, String] = ListMap.empty[String,String]
println(s"Empty ListMap of type String = $emptyListMap")

You should see the following output when you run your Scala application in IntelliJ:


Step 7: How to initialize an empty ListMap
Empty ListMap of type String = Map()

This concludes our tutorial on Learn How To Use Scala's Mutable ListMap 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 ListMap
  • How to initialize ListMap using key -> value notation
  • How to access elements of ListMap by specific key
  • How to add elements to ListMap using +=
  • How to add elements from a ListMap to an existing ListMap using ++=
  • How to remove key and its value from ListMap using -=
  • How to initialize an empty ListMap

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 LinkedHashMap.

 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: