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

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

Overview

In this tutorial, we will learn how to use Scala's Immutable ListMap and perform common operations such as initialization, appending or prepending elements and accessing elements within the 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 Immutable ListMap will be discussed in Chapter 8 on Collection Functions.

Steps

1. How to initialize a ListMap with 3 elements using key -> value notation

The code below shows how to initialize a ListMap with 3 elements using key -> value notation.


println("Step 1: How to initialize a ListMap with 3 elements using key -> value notation")
import scala.collection.immutable.ListMap
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 using key -> value notation
Elements of listMap1 = Map(PD -> Plain Donut, SD -> Strawberry Donut, CD -> Chocolate Donut)

 

2. How to access elements by specific key in the ListMap

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


println("\nStep 2: How to access elements by specific key in the ListMap")
println(s"Element by key PD = ${listMap1("PD")}")
println(s"Element by key SD = ${listMap1("SD")}")

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


Step 2: How to access elements by specific key in the ListMap
Element by key PD = Plain Donut
Element by key SD = Strawberry Donut

 

3. How to add elements to ListMap using +

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


println("\nStep 3: How to add elements to ListMap using +")
val listMap2: ListMap[String, String] = listMap1 + ("KD" -> "Krispy Kreme Donut")
println(s"Elements of listMap2 = $listMap2")

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


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

 

4. How to add two ListMaps together using ++

The code below shows how to add two ListMaps together using ++


println("\nStep 4: How to add two ListMaps together using ++")
val listMap3: ListMap[String, String] = listMap1 ++ listMap2
println(s"Elements of listMap3 = $listMap3")

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


Step 4: How to add two ListMaps together using ++
Elements of listMap3 = Map(PD -> Plain Donut, SD -> Strawberry Donut, CD -> Chocolate Donut, KD -> Krispy Kreme Donut)

 

5. How to remove key and value from ListMap using -

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


println("\nStep 5: How to remove key and value from ListMap using -")
val listMap4: ListMap[String, String] = listMap1 - ("CD")
println(s"ListMap without the key CD and its value = $listMap4")

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


Step 5: How to remove key and value from ListMap using -
ListMap without the key CD and its value = Map(PD -> Plain Donut, SD -> Strawberry Donut)

 

6. How to initialize an empty ListMap

The code below shows how to initialize an empty ListMap


println("\nStep 6: How to initialize an empty ListMap")
val emptyListMap: ListMap[String, String] = ListMap.empty[String,String]
println(s"Empty ListMap with key type String and value also of type String= $emptyListMap")

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


Step 6: How to initialize an empty ListMap
Empty ListMap with key type String and value also of type String= Map()

 

This concludes our tutorial on Learn How To Use Scala's Immutable 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 with 3 elements using key -> value notation
  • How to access elements by specific key in the ListMap
  • How to add elements to ListMap using +
  • How to add two ListMaps together using ++
  • How to remove key and 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 Immutable Map.

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: