Introduction to Scala Vector
Scala Vector is a new collection type in Scala introduced in Scala 2.8 to address random access in lists. It is an immutable data structure that provides random access to elements. Vector addresses the inefficiency of random access in the lists. It allows accessing the elements in constant time, we can access the elements and modify them easily with the help of vector.
Extending the abstract class, Abstract Seq and Indexed Seq trait Vectors are good for a large collection of elements. It has an indexed based immutable sequence in which we can easily access the elements. Update and selection are comparatively faster in Scala Vector.
Syntax
The definition of Scala Vector comes something like this extending AbstractSeq[A] with IndexedSeq[A] and so on as shown below. :-
final classVector[+A] extends AbstractSeq[A] with IndexedSeq[A] with GenericTraversableTemplate[A, Vector] with IndexedSeqLike[A, Vector[A]] with VectorPointer[A] with Serializable with CustomParallelizable[A, ParVector[A]]
Creating Vectors
scala> val a = Vector(1,2,3,4,5)
a: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5)
scala> val b = Vector("Arpit","Anand")
b: scala.collection.immutable.Vector[String] = Vector(Arpit, Anand)
Empty Vectors
We can create an Empty Vector also just by passing no values and defining the data type of the Vector as shown.
scala> val a = Vector[Int]()
a: scala.collection.immutable.Vector[Int] = Vector()
Creating new Vector By Populating its value
We can create a new vector just by populating values into to using various methods .
1. To and Until Method
scala> (3 to 5).toVector
res0: Vector[Int] = Vector(3, 4, 5)
scala> (1 until 10).toVector
res1: Vector[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9)
2. Range Method
scala> Vector.range(1 ,5)
res3: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4)
So these are the various method used to create an Scala Vector. There are some other methods also which we can use to create a Vector. Fill and tabulate , lets see how that works
3. Fill and Tabulate Method
scala> Vector.fill(4)("Anand")
Vector(Anand, Anand, Anand, Anand)
scala> Vector.tabulate(4)(n=>n+2 )
Vector(2, 3, 4, 5)
Code snippet for vector creation:
How Vector Function Works?
The data structure used by Vector is Trie. Trie is basically a 32-tree , Level 1 is an array of size 32 , level 2 is an array of 32 level 1’s and this goes on continually. It is independent of data type it want to.
Each 32-way node uses Array[32] can store either 0-32 reference’s to child nodes or 0-32 pieces of data.
- The position in the tree of that data structure defines the key with which it is associated and the root is associated with the empty string.
- The tree is structured in such a way that if the tree is n level deep then 1 to n-1 doesn’t not contains data the data will be contained only at level n.
Vector Methods
There are several Methods that we can be use in Scala Vector. Let us see about these methods in details.
1. Append and Prepend
We can append or prepend values in a vector making it convenient for programming models to add data values.
scala> val a = Vector(1,2,3,4,5,6)
Vector(1, 2, 3, 4, 5, 6)
scala> val b = a :+7
Vector(1, 2, 3, 4, 5, 6, 7)
scala> val b = a ++ Seq(7,8)
Vector(1, 2, 3, 4, 5, 6, 7, 8)
scala> val c = 3 +: a
Vector(3, 1, 2, 3, 4, 5, 6)
Code snippet:
2. Distinct Method
This returns a new sequence with no duplicate values.
scala> val a = Vector(1,2,3,4,5,6,6)
Vector(1, 2, 3, 4, 5, 6, 6)
scala> a.distinct
Vector(1, 2, 3, 4, 5, 6)
Code snippet:
3. Drop Method
Drops the first n elements given in the Vector.
scala> a.drop(2)
Vector(3, 4, 5, 6, 6)
4. Head Method
Returns the head , the first value of a Vector.
scala> a.head
res20: Int = 1
5. Tail Method
Returns the tail except for the first value of the Vector.
scala> a.tail
Vector(2, 3, 4, 5, 6, 6)
6. MAP in Vector
This method helps to return a new sequence by applying the function to each element.
scala> a.map(_*2)
Vector(2, 4, 6, 8, 10, 12, 12)
Code snippet:
7. Reverse Method
It reverse the vector value and produces a new value.
scala> a.reverse
Vector(6, 6, 5, 4, 3, 2, 1)
8. Union Method
It merges the value of one vector with other one and produces a new vector.
scala> val a = Vector(1,2,3,4,5,6,6)
Vector(1, 2, 3, 4, 5, 6, 6)
scala> val b = Vector(2,3,4)
Vector(2, 3, 4)
scala> a.union(b)
Vector(1, 2, 3, 4, 5, 6, 6, 2, 3, 4)
9. Empty Method
This will check whether the vector is EMPTY or not.
scala> a.isEmpty
res25: Boolean = false
10. Size Method
This returns the size of the Vector.
scala> a.size
res29: Int = 7
So here we saw some of the methods that are used with Scala Vector making it easy for conventional Scala programming.
Conclusion
From the above article, we knew about the collection type Vector and the advantage that It haves over functional programming. We saw some methods that we can use and the data structure used behind the vector method. So this level approach is good for functional programming in Scala.
Recommended Articles
We hope that this EDUCBA information on “Scala Vector” was beneficial to you. You can view EDUCBA’s recommended articles for more information.