Scala Tutorial - Learn How To Use Scala's Immutable List
Overview
In this tutorial, we will learn how to use Scala's Immutable List 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 List will be discussed in Chapter 8 on Collection Functions.
Steps
1. How to initialize an immutable List with 3 elements
The code below shows how to initialize an immutable List with 3 elements.
println("Step 1: How to initialize an immutable List with 3 elements")
val list1: List[String] = List("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of list1 = $list1")
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 list1 = List(Plain Donut, Strawberry Donut, Chocolate Donut)
2. How to access elements of immutable List at specific index
The code below shows how to access elements of immutable List at specific index.
println("\nStep 2: How to access elements of immutable List at specific index")
println(s"Element at index 0 = ${list1(0)}")
println(s"Element at index 1 = ${list1(1)}")
println(s"Element at index 2 = ${list1(2)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to access elements of immutable List at specific index
Element at index 0 = Plain Donut
Element at index 1 = Strawberry Donut
Element at index 2 = Chocolate Donut
3. How to append elements at the end of immutable List using :+
The code below shows how to append elements at the end of immutable List using :+
println("\nStep 3: How to append elements at the end of immutable List using :+")
val list2: List[String] = list1 :+ "Vanilla Donut"
println(s"Append elements at the end using :+ = $list2")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to append elements at the end of immutable List using :+
Append elements at the end using :+ = List(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)
4. How to prepend elements at the front of immutable List using +:
The code below shows how to prepend elements at the front of immutable List using +:
println("\nStep 4: How to prepend elements at the front of immutable List using +:")
val list3: List[String] = "Vanilla Donut" +: list1
println(s"Prepend elements at the front using +: = $list3")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to prepend elements at the front of immutable List using +:
Prepend elements at the front using +: = List(Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)
5. How to add two immutable lists together using ::
The code below shows how to add two immutable lists together using ::
println("\nStep 5: How to add two immutable lists together using ::")
val list4: List[Any] = list1 :: list2
println(s"Add two lists together using :: = $list4")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to add two immutable lists together using ::
Add two lists together using :: = List(List(Plain Donut, Strawberry Donut, Chocolate Donut), Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)
NOTE:
- Using :: returns a new List(List(...), elements from the second list)
6. How to add two immutable Lists together using :::
The code below shows how to add two immutable Lists together using :::
println("\nStep 6: How to add two immutable Lists together using :::")
val list5: List[String] = list1 ::: list2
println(s"Add two lists together using ::: = $list5")
You should see the following output when you run your Scala application in IntelliJ:
Step 6: How to add two immutable Lists together using :::
Add two lists together using ::: = List(Plain Donut, Strawberry Donut, Chocolate Donut, Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)
7. How to initialize an empty immutable List
The code below shows how to initialize an empty immutable List.
println("\nStep 7: How to initialize an empty immutable List")
val emptyList: List[String] = List.empty[String]
println(s"Empty list = $emptyList")
You should see the following output when you run your Scala application in IntelliJ:
Step 7: How to initialize an empty immutable List
Empty list = List()
Summary
In this tutorial, we went over the following:
- How to initialize an immutable List with 3 elements
- How to access elements of immutable List at specific index
- How to append elements at the end of immutable List using :+
- How to prepend elements at the front of immutable List using +:
- How to add two immutable lists together using ::
- How to add two immutable Lists together using :::
- How to initialize an empty immutable List
Tip
- Don't forget to review Chapter 8 on Collection Functions where we will go over the rich suite of functions exposed on collections such as aggregate, collect, fold, reduce, map, flatMap, scan, partition etc
- Scala's documentation on immutable List.
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 ListSet.