Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

<< Back to SCALA

Scala Vector Examples

Use Vector from scala.collection.immutable.Vector. Add elements and use Lists with vectors.
Vector. In Scala a vector is an immutable collection. But it can be added to, and updated, fast. It is represented with a tree of nodes.
Unlike an array, a vector's elements cannot be reassigned. But with update() we can change elements in a vector, copying only the nodes needed.
First example. Let us gain familiarity with Scala vector syntax. We use a ":+" operator to add a value to the end of a vector. We start with Vector.empty.

Append: We can add one or more elements in a single statement with the append operator. A separate operator is used for "prepend."

Prepend: This adds elements to the start of the vector. It is "+:" and the plus comes first for prepend.

Println: We use println to display the contents of a vector. A for-loop can be used to access and print all individual elements.

Scala program that uses Vector, adds to end and start // Begin with an empty vector. val vector = scala.collection.immutable.Vector.empty println(vector) // Add new value at end of vector. val vector2 = vector :+ 5 println(vector2) // Add 2 new values at end of vector. val vector3 = vector2 :+ 10 :+ 20 println(vector3) // Add new value at start of vector. val vector4 = 100 +: vector3 println(vector4) Output Vector() Vector(5) Vector(5, 10, 20) Vector(100, 5, 10, 20)
Add List. Sometimes we want to add an entire List to a Vector. We want to place all the elements from the List into a vector in one operation. The "++" operator helps here.List
Scala program that adds List elements to vector val v = scala.collection.immutable.Vector.empty println(v) // Add elements from List of Ints to end of vector. val v2 = v ++ List(10, 20, 30) println(v2) Output Vector() Vector(10, 20, 30)
Update. Here we update a string vector. We add 3 strings (cat, bird and frog) to the end of an empty vector. And then we update the element at index 1 to equal "fish."

Val: These vectors are immutable values, so we use the val keyword. When we call update, only some parts of v2 are updated and copied.

Scala program that uses update val v = scala.collection.immutable.Vector.empty println(v) // Add 3 strings to an empty vector. val v2 = v :+ "cat" :+ "bird" :+ "frog" println(v2) // Update element at index 1. val changed = v2.updated(1, "fish") println(changed) Output Vector() Vector(cat, bird, frog) Vector(cat, fish, frog)
For-loop. Looping over a collection is essential. Here we use a for-loop to enumerate the elements in a vector. We encounter all 3 Int values and print them.
Scala program that uses for loop on vector // Create an Int vector. // ... Append 3 elements. val v = scala.collection.immutable.Vector.empty :+ 5 :+ 10 :+ 50 println(v) // Loop over the vector's elements. for (e <- v) { println(e) } Output Vector(5, 10, 50) 5 10 50
Vector benchmark. The performance of a Vector is complicated to measure. But in this simple benchmark, I found that Vector has more overhead, and is slower, for small collections.

Version 1: This version of the code appends 2 elements to a vector, and then prepends 1 element to the start.

Version 2: This code appends twice and then prepends once to a List instead of a vector.

Result: The operations take about twice as long with a Vector. So for simple stuff a Vector is slower than a List.

Scala program that tests Vector, List performance // Warm up the JIT. val testVector = scala.collection.immutable.Vector.empty :+5 :+ 10 val testVector2 = 100 +: testVector val testList = List(5, 10) val testList2 = 100 :: testList val t1 = System.currentTimeMillis() // Version 1: add three elements to a vector. for (i <- 0 to 1000000) { val vector = scala.collection.immutable.Vector.empty :+5 :+ 10 val vector2 = 100 +: vector if (vector2.length != 3) println(vector2.length) } val t2 = System.currentTimeMillis() // Version 2: add three elements to a list. for (i <- 0 to 1000000) { val list = List(5, 10) val list2 = 100 :: list if (list2.length != 3) println(list2.length) } val t3 = System.currentTimeMillis() // Results. println(t2 - t1) println(t3 - t2) Output 125 ms, Vector 94 ms, List
Vectors are complex. They are an advanced collection, and in some sense they are an efficient, immutable array. For large collections of Ints or similar values, they are a good choice.

But: For top performance, operations that update a mutable array are faster than a vector. This operation, though, is optimized in vectors.

Tip: I recommend studying the Scala documentation about vectors when using them.

Quote: Because vectors strike a good balance between fast random selections and fast random functional updates, they are currently the default implementation of immutable indexed sequences.

A summary. Scala includes collections. These introduce a considerable amount of flexibility. A vector fills a requirement for an immutable, easily-updated array with fast element access.
The Dev Codes