
- Scala Collections - Home
- Scala Collections - Overview
- Scala Collections - Environment Setup
- Scala Collections - Arrays
- Scala Collections - Array
- Scala Collections - Multi-Dimensional Array
- Scala Collections - Array using Range
- Scala Collections - ArrayBuffer
- Scala Collections - Lists
- Scala Collections - List
- Scala Collections - ListBuffer
- Scala Collections - ListSet
- Scala Collections - Vector
- Scala Collections - Sets
- Scala Collections - Set
- Scala Collections - BitSet
- Scala Collections - HashSet
- Scala Collections - TreeSet
- Scala Collections - Maps
- Scala Collections - Map
- Scala Collections - HashMap
- Scala Collections - ListMap
- Scala Collections - Miscellaneous
- Scala Collections - Iterator
- Scala Collections - Option
- Scala Collections - Queue
- Scala Collections - Tuple
- Scala Collections - Seq
- Scala Collections - Stack
- Scala Collections - Stream
- Scala Collections Combinator methods
- Scala Collections - drop
- Scala Collections - dropWhile
- Scala Collections - filter
- Scala Collections - find
- Scala Collections - flatMap
- Scala Collections - flatten
- Scala Collections - fold
- Scala Collections - foldLeft
- Scala Collections - foldRight
- Scala Collections - map
- Scala Collections - partition
- Scala Collections - reduce
- Scala Collections - scan
- Scala Collections - zip
- Scala Collections Useful Resources
- Scala Collections - Quick Guide
- Scala Collections - Useful Resources
- Scala Collections - Discussion
Scala Collections - Vector
Scala Vector is a general purpose immutable data structure where elements can be accessed randomly. It is generally used for large collections of data.
Declaring Vector Variables
The following is the syntax for declaring an Vector variable.
Syntax
var z : Vector[String] = Vector("Zara","Nuha","Ayan")
Here, z is declared as an vector of Strings which has three members. Values can be added by using commands like the following −
Command
var vector1: Vector[String] = z + "Naira";
Processing Vector
Below is an example program of showing how to create, initialize and process Vector −
Example
import scala.collection.immutable.Vector object Demo { def main(args: Array[String]) = { var vector: Vector[String] = Vector("Zara","Nuha","Ayan"); // Add an element var vector1: Vector[String] = vector :+ "Naira"; // Reverse an element var vector2: Vector[String] = vector.reverse; // sort a vector var vector3: Vector[String] = vector1.sorted; println(vector); println(vector1); println(vector2); println(vector3); } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
Vector(Zara, Nuha, Ayan) Vector(Zara, Nuha, Ayan, Naira) Vector(Ayan, Nuha, Zara) Vector(Ayan, Naira, Nuha, Zara)
Accessing Elements
You can access elements in a Vector using their indices. It is a constant-time operation. So, Vectors are efficient for random access.
Example
object Demo { def main(args: Array[String]) = { var vector: Vector[String] = Vector("Zara", "Nuha", "Ayan") println("First element: " + vector(0)) println("Second element: " + vector(1)) println("Third element: " + vector(2)) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
First element: Zara Second element: Nuha Third element: Ayan
Updating Elements
Since vectors are immutable. So, if you update an element, it creates a new Vector with the updated value. Try following example for updating elements -
Example
object Demo { def main(args: Array[String]) = { var vector: Vector[String] = Vector("Zara", "Nuha", "Ayan") var updatedVector: Vector[String] = vector.updated(1, "Naira") println("Original vector: " + vector) println("Updated vector: " + updatedVector) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Original vector: Vector(Zara, Nuha, Ayan) Updated vector: Vector(Zara, Naira, Ayan)
Concatenating Vectors
You can concatenate two or more Vectors using the ++ operator. You can also use the Vector.++() method for concatenating vectors. Try following example for concatenating vectors -
Example
object Demo { def main(args: Array[String]) = { var vector1: Vector[String] = Vector("Zara", "Nuha") var vector2: Vector[String] = Vector("Ayan", "Naira") // Use ++ operator var concatenatedVector = vector1 ++ vector2 println("vector1 ++ vector2: " + concatenatedVector) // Use ++ method concatenatedVector = vector1.++(vector2) println("vector1.++(vector2): " + concatenatedVector) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
vector1 ++ vector2: Vector(Zara, Nuha, Ayan, Naira) vector1.++(vector2): Vector(Zara, Nuha, Ayan, Naira)
Iterating over Vectors
You can iterate over elements in a Vector using a foreach loop or other iteration methods.
Example
Try following example for iterating over a vector -
object Demo { def main(args: Array[String]) = { var vector: Vector[String] = Vector("Zara", "Nuha", "Ayan") vector.foreach { element => println(element) } } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Zara Nuha Ayan
Using map() Method
You can transform a Vector using map() function to each element. This is used for performing operations on each element and creating a new Vector with the transformed elements.
Example
Try following example for using map() method -
object Demo { def main(args: Array[String]) = { var vector: Vector[Int] = Vector(1, 2, 3) var doubledVector: Vector[Int] = vector.map(_ * 2) println("Doubled vector: " + doubledVector) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Doubled vector: Vector(2, 4, 6)
Using filter() Method
You can filter elements using the filter() method in a Vector based on a predicate function. It creates a new Vector containing only the elements that satisfy the given predicate.
Example
Try following example for using filter() method -
object Demo { def main(args: Array[String]) = { var vector: Vector[Int] = Vector(1, 2, 3, 4, 5) var evenVector: Vector[Int] = vector.filter(_ % 2 == 0) println("Even vector: " + evenVector) } }
Save the above program in Demo.scala. Use the following commands to compile and execute this program.
Command
> scalac Demo.scala > scala Demo
Output
Even vector: Vector(2, 4)
Scala Vector Summary
- Vectors are immutable data structures. These are optimized for random access and large collections. Vectors are also efficient for updates and appends.
- You can create vectors using the Vector companion object and add elements using the :+
- You can perform various operations on vectors, like reversing, sorting, and mapping.
- You can also perform operations, like concatenation, finding the length, retrieving elements at specific positions, and checking if the vector contains certain elements.
- There are various vector methods, like, head, tail, isEmpty, foreach, map, flatMap, filter, and fold.