C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
PrintlnScala 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)
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)
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.
ValScala 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)
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
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
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.
Collections: scala-lang.org