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

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

Overview

In this tutorial, we will learn how to use Scala's Immutable Sequence and perform common operations such as initialization, adding elements, adding Sequences and creating empty Sequence.

 

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

Steps

1. How to initialize a Sequence with 3 elements

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


println("Step 1: How to initialize a Sequence with 3 elements")
val seq1: Seq[String] = Seq("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of seq1 = $seq1")

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


Step 1: How to initialize a Sequence with 3 elements
Elements of seq1 = List(Plain Donut, Strawberry Donut, Chocolate Donut)

 

NOTE:

  • Sequence is a trait and if you look carefully in the console window for Step 1 above, the elements of the Sequence were rendered into a concrete Immutable List.

2. How to access elements in Sequence at specific index

The code below shows how to access elements in Sequence at specific index.


println("\nStep 2: How to access elements in Sequence at specific index")
println(s"Element at index 0 = ${seq1(0)}")
println(s"Element at index 1 = ${seq1(1)}")
println(s"Element at index 2 = ${seq1(2)}")

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


Step 2: How to access elements in Sequence at specific index
Element at index 0 = Plain Donut
Element at index 1 = Strawberry Donut
Element at index 2 = Chocolate Donut

 

3. How to add elements to Sequence using :+

The code below shows how to add elements to Sequence using :+.


println("\nStep 3: How to add elements to Sequence using :+")
val seq2: Seq[String] = seq1 :+ "Vanilla Donut"
println(s"Adding elements to Sequence using :+ = $seq2")

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


Step 3: How to add elements to Sequence using :+
Adding elements to Sequence using :+ = List(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)

 

4. How to add two Sequence together using ++

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


println("\nStep 4: How to add two Sequence together using ++")
val seq3: Seq[String] = seq1 ++ Seq[String]("Vanilla Donut", "Glazed Donut")
println(s"Add two sequences together using ++ = $seq3")

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


Step 4: How to add two Sequence together using ++
Add two sequences together using ++ = List(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut, Glazed Donut)

 

5. How to initialize an empty Sequence

The code below shows how to initialize an empty Sequence.


println("\nStep 5: How to initialize an empty Sequence")
val emptySeq: Seq[String] = Seq.empty[String]
println(s"Empty Sequence = $emptySeq")

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


Step 5: How to initialize an empty Sequence
Empty Sequence = List()

 

This concludes our tutorial on Learn How To Use Scala's Immutable Sequence 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 Sequence with 3 elements
  • How to access elements in Sequence at specific index
  • How to add elements to Sequence using :+
  • How to add two Sequence together using ++
  • How to initialize an empty Sequence

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

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: