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

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 ListSet and perform common operations such as initialization, appending or prepending elements and accessing elements within the List.

 

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

Steps

1. How to initialize an immutable ListSet with 3 elements

The code below shows how to initialize an immutable ListSet with 3 elements.


println("Step 1: How to initialize an immutable ListSet with 3 elements")
val listSet1: ListSet[String] = ListSet("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of listSet1 = $listSet1")

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


Step 1: How to initialize an immutable List with 3 elements
Elements of listSet1 = ListSet(Chocolate Donut, Strawberry Donut, Plain Donut)

 

2. How to check elements of immutable ListSet

The code below shows How to check elements of immutable ListSet.


println("\nStep 2: How to check elements of immutable ListSet")
println(s"Element Plain Donut = ${listSet1("Plain Donut")}")
println(s"Element Strawberry Donut = ${listSet1("Strawberry Donut")}")
println(s"Element Chocolate Donut = ${listSet1("Chocolate Donut")}")

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


Step 2: How to check elements of immutable ListSet
Element Plain Donut = true
Element Strawberry Donut = true
Element Chocolate Donut = true

 

3. How to add elements of immutable ListSet using +

The code below shows how to add elements of immutable ListSet using +


println("\nStep 3: How to add elements of immutable ListSet using +")
val listSet2: ListSet[String] = listSet1 + "Vanilla Donut"
println(s"Adding element Vanilla to ListSet using + = $listSet2")

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


Step 3: How to add elements of immutable ListSet using +
Adding element Vanilla to ListSet using + = ListSet(Vanilla Donut, Chocolate Donut, Strawberry Donut, Plain Donut)

 

4. How to add two ListSet together using ++

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


println("\nStep 4: How to add two ListSet together using ++")
val listSet3: ListSet[String] = listSet1 ++ ListSet("Glazed Donut")
println(s"Add two lists together using ++ = $listSet3")

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


Step 4: How to add two ListSet together using ++
Add two lists together using ++ = ListSet(Glazed Donut, Chocolate Donut, Strawberry Donut, Plain Donut)

 

5. How to remove element from the ListSet using -

The code below shows how to remove element from the ListSet using -


println("\nStep 5: How to remove element from the ListSet using -")
val listSet4: ListSet[String] = listSet1 - ("Plain Donut")
println(s"ListSet without the element Plain Donut = $listSet4")

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


Step 5: How to remove element from the ListSet using -
ListSet without the element Plain Donut = ListSet(Chocolate Donut, Strawberry Donut)

 

6. How to initialize an empty ListSet

The code below shows how to initialize an empty ListSet


println("\nStep 6: How to initialize an empty ListSet")
val emptyListSet: ListSet[String] = ListSet.empty[String]
println(s"Empty ListSet of type String = $emptyListSet")

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


Step 6: How to initialize an empty ListSet
Empty ListSet of type String = ListSet()

 

This concludes our tutorial on Learn How To Use Scala's Immutable ListSet 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 an immutable ListSet with 3 elements
  • How to check elements of immutable ListSet
  • How to add elements of immutable ListSet using +
  • How to add two ListSet together using ++
  • How to remove element from the ListSet using -
  • How to initialize an empty ListSet

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

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: