VOOZH about

URL: https://www.geeksforgeeks.org/scala/scala-vector/

⇱ Scala - Vector - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scala - Vector

Last Updated : 12 Jul, 2025

Scala is an object-oriented programming language with functional and multi-paradigm support. Scala generates byte code and runs on Java Virtual Machine. Vectors in scala are immutable data structures providing random access for elements and is similar to the list. But, the list has incompetence of random access of elements.  Below is an implementation of some of the operations performed on vectors in Scala:
 

1. Creating a vector: A new vector can be created in Scala using Vector() function and providing the elements in the parenthesis. 


Example:
 


Output:
Vector(2, 3, 4, 5)
2 3 4 5 

2. Adding elements to the vector: A single element can be added to the vector in Scala using :+ operator and multiple elements can be added in the vector using ++ operator. 


Example: 
 


Output:
Vector Elements after adding: 2 3 4 5 10 
Vector Elements after merging: 2 3 4 5 10 7 100 

3. Reversing vector elements: Elements of a vector can be reversed in the order they are inserted using reverse function present in scala.collection.immutable package. 


Example: 
 


Output:
Vector elements before reversing: 2 3 4 5 
Vector Elements after reversing: 5 4 3 2 

4. Sorting elements of vector: Elements of a vector can be sorted using sorted function in Scala. 


Example: 
 


Output:
Vector elements before sorting:5 1 9 100 2 25 17 
Vector Elements after sorting: 1 2 5 9 17 25 100 
Comment
Article Tags:

Explore