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

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:38 pm

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.

 

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
Senior Software Developer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on