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

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 LinkedHashMap to perform common operations such as initialize a LinkedHashMap, access elements by key, add and remove elements and create an empty LinkedHashMap.

 

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

 

What is a LinkedHashMap?

As per the Scala Documentation, a LinkedHashMap is a collection of key and value pairs which are stored internally using Hash Table data structure. But iterating through the elements is done in order.

 

Steps

1. How to initialize a LinkedHashMap with 3 elements

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


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

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

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

 

2. How to initialize a LinkedHashMap using key -> value notation

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


println("\nStep 2: How to initialize a LinkedHashMap using key -> value notation")
val linkedHashMap2: LinkedHashMap[String, String] = LinkedHashMap("VD"-> "Vanilla Donut", "GD" -> "Glazed Donut")
println(s"LinkedHashMap1 = $linkedHashMap2")

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

Step 2: How to initialize a LinkedHashMap using key -> value notation
LinkedHashMap1 = Map(VD -> Vanilla Donut, GD -> Glazed Donut)

3. How to access elements of LinkedHashMap by specific key

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


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

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


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

 

4. How to add elements to LinkedHashMap using +=

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


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

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

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

 

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

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

println("\nStep 5: How to add elements from a LinkedHashMap to an existing LinkedHashMap using ++=")
linkedHashMap1 ++= linkedHashMap2
println(s"Elements of linkedHashMap1 = ${linkedHashMap1}")

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

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

 

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

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


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

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


Step 6: How to remove key and its value from LinkedHashMap using -=
LinkedHashMap 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 LinkedHashMap

The code below shows how to initialize an empty LinkedHashMap.


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

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


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

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

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

 

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: