Scala Tutorial - Learn How To Use Scala's Immutable Vector
Overview
In this tutorial, we will learn how to use Scala's Immutable Vector to perform common operations such as initialize a Vector, access elements at specific index, append and prepend elements to Vector, and create an empty Vector.
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 Vector will be discussed in Chapter 8 on Collection Functions.
What is a Vector?
As per the Scala Documentation, a Vector is data structure which is similar to a List. However, it addresses the inefficiencies of random access within a List.
Steps
1. How to initialize a Vector with 3 elements
The code below shows how to initialize a Vector with 3 elements.
println("Step 1: How to initialize a Vector with 3 elements")
val vector1: Vector[String] = Vector("Plain Donut", "Strawberry Donut", "Chocolate Donut")
println(s"Elements of vector1 = $vector1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a Vector with 3 elements
Elements of vector1 = Vector(Plain Donut, Strawberry Donut, Chocolate Donut)
2. How to access elements of Vector at specific index
The code below shows how to access elements of Vector at specific index.
println("\nStep 2: How to access elements of Vector at specific index")
println(s"Element at index 0 = ${vector1(0)}")
println(s"Element at index 1 = ${vector1(1)}")
println(s"Element at index 2 = ${vector1(2)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to access elements of Vector 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 Vector using :+
The code below shows how to append elements at the end of Vector using :+.
println("\nStep 3: How to append elements at the end of Vector using :+")
val vector2 = vector1 :+ "Vanilla Donut"
println(s"Adding elements to Vector using :+ = $vector2")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to append elements at the end of Vector using :+
Adding elements to Vector using :+ = Vector(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)
4. How to prepend elements in front of Vector using +:
The code below shows how to prepend elements in front of Vector using +:.
println("\nStep 4: How to prepend elements in front of Vector using +:")
val vector3 = "Vanilla Donut" +: vector1
println(s"Adding elements to Vector using :+ = $vector3")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to prepend elements in front of Vector using +:
Adding elements to Vector using :+ = Vector(Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)
5. How to add two Vectors together using ++
The code below shows how to add two Vectors together using ++.
println("\nStep 5: How to add two Vectors together using ++")
val vector4 = vector1 ++ Vector[String]("Glazed Donut")
println(s"Add two vectors together using ++ = $vector3")
// NOTE: this return a new Vector(...), elements from the second vector)
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to add two Vectors together using ++
Add two vectors together using ++ = Vector(Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)
6. How to initialize an empty Vector
The code below shows how to initialize an empty Vector.
println("\nStep 6: How to initialize an empty Vector")
val emptyVector: Vector[String] = Vector.empty[String]
println(s"Empty vector of type String = $emptyVector")
You should see the following output when you run your Scala application in IntelliJ:
Step 6: How to initialize an empty Vector
Empty vector of type String = Vector()
Summary
In this tutorial, we went over the following:
- How to initialize a Vector with 3 elements
- How to access elements of Vector at specific index
- How to append elements at the end of Vector using :+
- How to prepend elements in front of Vector using +:
- How to add two Vectors together using ++
- How to initialize an empty Vector
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 Vector.
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
If you have followed the previous tutorials, you should by now feel more comfortable with using the Immutable Collection in Scala.
This would be a good place to proceed to Chapter 7 where I will go over the Mutable Collection in Scala.