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

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

Overview

In this tutorial, we will learn how to use Scala's Mutable LinkedHashSet to perform common operations such as initialize a LinkedHashSet, check specific elements, add and remove elements, LinkedHashSet intersection and difference, access and print elements in order they were inserted to LinkedHashSet, and create an empty LinkedHashSet.

 

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

 

What is a LinkedHashSet?

As per Wikipedia, a Set is a data structure which allows you to store elements which are not repeatable. A Set also does not guarantee the ordering of elements.

 

As per the Scala Documentation, a LinkedHashSet will produce elements in order they were inserted to the LinkedHashSet. As mentioned in the previous tutorials, TreeSet and SortedSet allow you to drive the order of the elements. TreeSet and SortedSet should not be confused with LinkedHashSet where the order of elements will be according to the insertion order of the elements.

Steps

1. How to initialize a LinkedHashSet with 3 elements

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


import scala.collection.mutable.LinkedHashSet
println("\nStep 1: How to initialize a LinkedHashSet with 3 elements")
val linkedHashSet1: LinkedHashSet[String] = LinkedHashSet("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of linkedHashSet1 = $linkedHashSet1")

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

Step 1: How to initialize a LinkedHashSet with 3 elements
Elements of linkedHashSet1 = Set(Plain Donut, Strawberry Donut, Chocolate Donut)

 

2. How to check specific elements in LinkedHashSet

The code below shows how to check specific elements in LinkedHashSet.


println("\nStep 2: How to check specific elements in LinkedHashSet")
println(s"Element Plain Donut = ${linkedHashSet1("Plain Donut")}")
println(s"Element Strawberry Donut = ${linkedHashSet1("Strawberry Donut")}")
println(s"Element Chocolate Donut = ${linkedHashSet1("Chocolate Donut")}")

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


Step 2: How to check specific elements in LinkedHashSet
Element Plain Donut = true
Element Strawberry Donut = true
Element Chocolate Donut = true

 

3. How to add elements to LinkedHashSet using +=

The code below shows how to add elements to LinkedHashSet using +=


println("\nStep 3: How to add elements to LinkedHashSet using +=")
linkedHashSet1 += "Vanilla Donut"
println(s"Elements of linkedHashSet1 after adding Vanilla Donut element = $linkedHashSet1")

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


Step 3: How to add elements to LinkedHashSet using +=
Elements of linkedHashSet1 after adding Vanilla Donut element = Set(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)

 

4. How to add two LinkedHashSets together using ++=

The code below shows how to add two LinkedHashSets together using ++=.


println("\nStep 4: How to add two LinkedHashSets together using ++=")
linkedHashSet1 ++= LinkedHashSet[String]("Vanilla Donut", "Glazed Donut")
println(s"Elements of linkedHashSet1 after adding another HashSet = $linkedHashSet1")

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

Step 4: How to add two LinkedHashSets together using ++=
Elements of linkedHashSet1 after adding another HashSet = Set(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut, Glazed Donut)

 

5. How to remove element from LinkedHashSet using -=

The code below shows how to remove element from LinkedHashSet using -=.

println("\nStep 5: How to remove element from LinkedHashSet using -=")
linkedHashSet1 -= "Plain Donut"
println(s"Set without Plain Donut element = $linkedHashSet1")

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

Step 5: How to remove element from LinkedHashSet using -=
Set without Plain Donut element = Set(Strawberry Donut, Chocolate Donut, Vanilla Donut, Glazed Donut)

 

6. How to find the intersection between two LinkedHashSets using &

The code below shows how to find the intersection between two LinkedHashSets using &.


println("\nStep 6: How to find the intersection between two LinkedHashSets using &")
val linkedHashSet2: LinkedHashSet[String] = LinkedHashSet("Vanilla Donut", "Glazed Donut", "Plain Donut")
println(s"Intersection of linkedHashSet1 and linkedHashSet2 = ${linkedHashSet1 & linkedHashSet2}")

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


Step 6: How to find the intersection between two LinkedHashSets using &
Intersection of linkedHashSet1 and linkedHashSet2 = Set(Vanilla Donut, Glazed Donut)

 

7. How to find the difference between two LinkedHashSets using &~

The code below shows how to find the difference between two LinkedHashSets using &~.


println("\nStep 7: How to find the difference between two LinkedHashSets using &~")
println(s"Difference of linkedHashSet1 and linkedHashSet2 = ${linkedHashSet1 &~ linkedHashSet2}")

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


Step 7: How to find the difference between two LinkedHashSets using &~
Difference of linkedHashSet1 and linkedHashSet2 = Set(Strawberry Donut, Chocolate Donut)

8. How to initialize an empty LinkedHashSet

The code below shows how to initialize an empty LinkedHashSet.


println("\nStep 8: How to initialize an empty LinkedHashSet")
val emptyLinkedHashSet: LinkedHashSet[String] = LinkedHashSet.empty[String]
println(s"Empty LinkedHashSet = $emptyLinkedHashSet")

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


Step 8: How to initialize an empty LinkedHashSet
Empty LinkedHashSet = Set()

 

9. How to print elements in order inserted to LinkedHashSet using foreach function

The code below shows how to print elements in order inserted to LinkedHashSet using foreach function.


println("\nStep 9: How to print elements in order inserted to LinkedHashSet using foreach function")
val linkedHashSet3: LinkedHashSet[String] = LinkedHashSet.empty[String]
linkedHashSet3 += "Vanilla Donut"
linkedHashSet3 += "Glazed Donut"
linkedHashSet3 += "Plain Donut"
linkedHashSet3 += "Chocolate Donut"
linkedHashSet3.foreach(donut => println(s"$donut"))

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


Step 9: How to print elements in order inserted to LinkedHashSet using foreach function
Vanilla Donut
Glazed Donut
Plain Donut
Chocolate Donut

This concludes our tutorial on Learn How To Use Scala's Mutable LinkedHashSet 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 LinkedHashSet
  • How to check specific elements in LinkedHashSet
  • How to add elements to LinkedHashSet using +=
  • How to add two LinkedHashSets together using ++=
  • How to remove element from LinkedHashSet using -=
  • How to find the intersection between two LinkedHashSets using &
  • How to find the difference between two LinkedHashSets using &~
  • How to initialize an empty LinkedHashSet
  • How to print elements in order inserted to LinkedHashSet using foreach function

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

 

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: