Table of Contents
- Scala Vector class video
- The Scala Vector class
- Notes about the following Vector examples
- CREATE: How to create a Vector with initial elements
- CREATE: Create a new Vector by populating it
- READ: How to access Vector elements
- UPDATE: How to add (append and prepend) elements
- UPDATE: How to “update” Vector elements using its methods
- DELETE: Filtering methods (how to “remove” elements from an Vector)
- MORE: Transformer methods
- MORE: Informational and mathematical methods
- MORE: Grouping methods
- Looping over a Vector with for and foreach
- A few things you can do with a Vector of Options
- Scala Vector summary
Table of Contents
- Vector class introduction
- Notes about the following Vector examples
- CREATE: How to create a Vector with initial elements
- CREATE: Create a new Vector by populating it
- READ: How to access Vector elements
- UPDATE: How to add (append and prepend) elements
- UPDATE: How to “update” Vector elements using its methods
- DELETE: Filtering methods (how to “remove” elements from an Vector)
- MORE: Transformer methods
- MORE: Informational and mathematical methods
- MORE: Grouping methods
- Looping over a Vector with for and foreach
- A few things you can do with a Vector of Options
- Scala Vector summary
This page contains over 170 examples of how to use the Scala Vector class, covering most of the methods that are available on a Vector, including map, filter, foldLeft, reduceLeft, and many more.
Scala Vector class video
As a brief update, if you prefer videos, here is my Scala Vector class training video.
The Scala Vector class
Use the Vector class when you want to use a general-purpose, immutable indexed sequence in your Scala applications:
- Immutable: the elements in the collection cannot be changed, and the collection cannot be resized
- Indexed: you can access elements quickly by their element number, such as people(12345432)(a linked list would be very slow for this purpose)
Just as the Scala ArrayBuffer is the recommended “go to” class for mutable indexed sequential collections, the Vector class is recommended as the general-purpose go-to class for immutable indexed sequential collections. To this point, the Vector class Scaladoc states:
“
Vectoris a general-purpose, immutable data structure. It provides random access and updates in O(log n) time, as well as very fast append/prepend/tail/init (amortized O(1), worst case O(log n)). 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.”
Notes about the following Vector examples
Remember that Vector is immutable, so in all of the examples that follow you need to assign the result of each expression to a new variable, like this:
val x = nums.distinct 
 As a second note, the following examples are demonstrated in a “CRUD+” order, meaning:
- C — Create
- R — Read
- U — Update
- D — Delete
- + — demonstrates many methods available on the Vector class
CREATE: How to create a Vector with initial elements
To create a new Vector with initial elements:
val nums = Vector(1, 2, 3)
case class Person(name: String)
val people = Vector(
    Person("Emily"),
    Person("Hannah"),
    Person("Mercedes")
) 
 When you need to be clear about what’s in the vector:
// Example 1:
val x = Vector(1, 1.0, 1F)                   // Vector[Double] = Vector(1.0, 1.0, 1.0)
val x: Vector[Number] = Vector(1, 1.0, 1F)   // Vector[Number] = Vector(1, 1.0, 1.0)
// Example 2:
trait Animal
trait HasTail
case class Dog(name: String) extends Animal, HasTail
case class Cat(name: String) extends Animal, HasTail
// be explicit about the type in the Vector:
val animalHouse: Vector[Animal] = Vector(
    Dog("Rover"),
    Cat("Felix")
)
// RESULT: Vector[Animal] = Vector(Dog(Rover), Cat(Felix))
 
 If you ever need to create an empty vector:
val nums = Vector[Int]() 
 Remember the constructor syntax is just syntactic sugar for apply:
val nums = Vector(1, 2, 3)                   // Vector(1, 2, 3)
val nums = Vector.apply(1, 2, 3)             // Vector(1, 2, 3) 
 CREATE: Create a new Vector by populating it
You can also create a new Vector that’s populated with initial elements using a Range:
// to, until
(1 to 5).toVector                   // Vector(1, 2, 3, 4, 5)
(1 until 5).toVector                // Vector(1, 2, 3, 4)
(1 to 10 by 2).toVector             // Vector(1, 3, 5, 7, 9)
(1 until 10 by 2).toVector          // Vector(1, 3, 5, 7, 9)
(1 to 10).by(2).toVector            // Vector(1, 3, 5, 7, 9)
('d' to 'h').toVector               // Vector(d, e, f, g, h)
('d' until 'h').toVector            // Vector(d, e, f, g)
('a' to 'f').by(2).toVector         // Vector(a, c, e)
// range method
Vector.range(1, 3)                  // Vector(1, 2)
Vector.range(1, 6, 2)               // Vector(1, 3, 5) 
 You can also use the fill and tabulate methods:
Vector.fill(3)("foo")               // Vector(foo, foo, foo)
Vector.tabulate(3)(n => n * n)      // Vector(0, 1, 4)
Vector.tabulate(4)(n => n * n)      // Vector(0, 1, 4, 9) 
 READ: How to access Vector elements
As with other Scala sequence classes, you access Vector elements by their index, inside parentheses:
val names = Vector("bert", "ernie", "oscar")
names(0)    // "bert"
names(1)    // "ernie"
names(2)    // "oscar" 
 You can also read/access Vector elements in for loops and for expressions, as shown here:
val oneToFive = Vector(1, 2, 3, 4, 5) for (i <- oneToFive) yield i // Vector(1, 2, 3, 4, 5) for (i <- oneToFive) yield i * 2 // Vector(2, 4, 6, 8, 10) for (i <- oneToFive) yield i % 2 // Vector(1, 0, 1, 0, 1)
UPDATE: How to add (append and prepend) elements
Because Vector is immutable, you can’t add elements to an existing Vector. The way you work with Vector is to modify the elements it contains as you assign the results to a new Vector.
| Method | Description | Example | 
|---|---|---|
|  :+ | append 1 item | oldSeq :+ e | 
|  ++ | append N items | oldSeq ++ newSeq | 
|  +: | prepend 1 item | e +: oldSeq | 
|  ++: | prepend N items | newSeq ++: oldSeq | 
Append and prepend examples
These examples show how to use the append and prepend methods:
val v1 = Vector(4,5,6)     // Vector(4, 5, 6)
val v2 = v1 :+ 7           // Vector(4, 5, 6, 7)
val v3 = v2 ++ Seq(8,9)    // Vector(4, 5, 6, 7, 8, 9)
val v4 = 3 +: v3           // Vector(3, 4, 5, 6, 7, 8, 9)
val v5 = Seq(1,2) ++: v4   // Vector(1, 2, 3, 4, 5, 6, 7, 8, 9) 
 About the : character in the method names
 
 Note that during these operations the : character is always next to the old (original) sequence. I use that as a way to remember these methods.
The correct technical way to think about this is that a Scala method name that ends with the : character is right-associative, meaning that the method comes from the variable on the right side of the expression. Therefore, with +: and ++:, these methods comes from the Vector that’s on the right of the method name.
UPDATE: How to “update” Vector elements using its methods
Because Vector is immutable, you can’t update elements in place, but depending on your definition of “update,” there are a variety of methods that let you update a Vector as you assign the result to a new variable:
| Method | Returns | 
|---|---|
| collect(pf) | A new collection by applying the partial function pfto all elements of the vector, returning elements for which the function is defined | 
| distinct | A new sequence with no duplicate elements | 
| flatten | Transforms a list of lists into a single list | 
| flatMap(f) | When working with sequences, it works like mapfollowed byflatten | 
| map(f) | Return a new sequence by applying the function fto each element in theVector | 
| updated(i,v) | A new vector with the element at index ireplaced with the new valuev | 
| union(s) | A new vector that contains elements from the current vector and the sequence s | 
val x = Vector(Some(1), None, Some(3), None)
x.collect{case Some(i) => i}          // Vector(1, 3)
val x = Vector(1,2,1,2)
x.distinct                            // Vector(1, 2)
x.map(_ * 2)                          // Vector(2, 4, 2, 4)
x.updated(0,100)                      // Vector(100, 2, 1, 2)
val a = Vector(Seq(1,2), Seq(3,4))
a.flatten                             // Vector(1, 2, 3, 4)
val fruits = Vector("apple", "pear")
fruits.map(_.toUpperCase)             // Vector(APPLE, PEAR)
fruits.flatMap(_.toUpperCase)         // Vector(A, P, P, L, E, P, E, A, R)
Vector(2,4).union(Vector(1,3))        // Vector(2, 4, 1, 3) 
 DELETE: Filtering methods (how to “remove” elements from an Vector)
A Vector is an immutable sequence, so you don’t remove elements from a Vector. Instead, you describe how to remove elements as you assign the results to a new collection. These methods let you “remove” elements during this process:
| Method | Description | 
|---|---|
| distinct | Return a new sequence with no duplicate elements | 
| drop(n) | Return all elements after the first nelements | 
| dropRight(n) | Return all elements except the last nelements | 
| dropWhile(p) | Drop the first sequence of elements that matches the predicate p | 
| filter(p) | Return all elements that match the predicate p | 
| filterNot(p) | Return all elements that do not match the predicate p | 
| find(p) | Return the first element that matches the predicate p | 
| head | Return the first element; can throw an exception if the Vectoris empty | 
| headOption | Returns the first element as an Option | 
| init | All elements except the last one | 
| intersect(s) | Return the intersection of the vector and another sequence s | 
| last | The last element; can throw an exception if the Vectoris empty | 
| lastOption | The last element as an Option | 
| slice(f,u) | A sequence of elements from index f(from) to indexu(until) | 
| tail | All elements after the first element | 
| take(n) | The first nelements | 
| takeRight(n) | The last nelements | 
| takeWhile(p) | The first subset of elements that matches the predicate p | 
Examples
val a = Vector(10, 20, 30, 40, 10)    // Vector(10, 20, 30, 40, 10)
a.distinct                            // Vector(10, 20, 30, 40)
a.drop(2)                             // Vector(30, 40, 10)
a.dropRight(2)                        // Vector(10, 20, 30)
a.dropWhile(_ < 25)                   // Vector(30, 40, 10)
a.filter(_ < 25)                      // Vector(10, 20, 10)
a.filter(_ > 100)                     // Vector()
a.filterNot(_ < 25)                   // Vector(30, 40)
a.find(_ > 20)                        // Some(30)
a.head                                // 10
a.headOption                          // Some(10)
a.init                                // Vector(10, 20, 30, 40)
a.intersect(Seq(19,20,21))            // Vector(20)
a.last                                // 10
a.lastOption                          // Some(10)
a.slice(2,4)                          // Vector(30, 40)
a.tail                                // Vector(20, 30, 40, 10)
a.take(3)                             // Vector(10, 20, 30)
a.takeRight(2)                        // Vector(40, 10)
a.takeWhile(_ < 30)                   // Vector(10, 20) 
 As noted, head and last can throw exceptions:
scala> val a = Vector[Int]()
a: scala.collection.immutable.Vector[Int] = Vector()
scala> a.head
java.lang.UnsupportedOperationException: empty.head
  at scala.collection.immutable.Vector.head(Vector.scala:185)
  ... 28 elided
scala> a.last
java.lang.UnsupportedOperationException: empty.last
  at scala.collection.immutable.Vector.last(Vector.scala:197)
  ... 28 elided 
 MORE: Transformer methods
A transformer method is a method that constructs a new collection from an existing collection.
| Method | Returns | 
|---|---|
| collect(pf) | Creates a new collection by applying the partial function pfto all elements of the vector, returning elements for which the function is defined | 
| diff(c) | The difference between this vector and the collection c | 
| distinct | A new sequence with no duplicate elements | 
| flatten | Transforms a list of lists into a single list | 
| flatMap(f) | When working with sequences, it works like mapfollowed byflatten | 
| map(f) | A new sequence by applying the function fto each element in theVector | 
| reverse | A new sequence with the elements in reverse order | 
| sortWith(f) | A new sequence with the elements sorted with the use of the function f | 
| updated(i,v) | A new Vectorwith the element at indexireplaced with the new valuev | 
| union(c) | A new sequence that contains all elements of the vector and the collection c | 
| zip(c) | A collection of pairs by matching the vector with the elements of the collection c | 
| zipWithIndex | A vector of each element contained in a tuple along with its index | 
val x = Vector(Some(1), None, Some(3), None)
x.collect{case Some(i) => i}          // Vector(1, 3)
// diff
val oneToFive = (1 to 5).toVector     // val oneToFive = (1 to 5).toVector
val threeToSeven = (3 to 7).toVector  // Vector(3, 4, 5, 6, 7)
oneToFive.diff(threeToSeven)          // Vector(1, 2)
threeToSeven.diff(oneToFive)          // Vector(6, 7)
Vector(1,2,1,2).distinct              // Vector(1, 2)
val a = Vector(Seq(1,2), Seq(3,4))
a.flatten                             // Vector(1, 2, 3, 4)
// map, flatMap
val fruits = Vector("apple", "pear")
fruits.map(_.toUpperCase)             // Vector(APPLE, PEAR)
fruits.flatMap(_.toUpperCase)         // Vector(A, P, P, L, E, P, E, A, R)
Vector(1,2,3).reverse                 // Vector(3, 2, 1)
val nums = Vector(10, 5, 8, 1, 7)
nums.sorted                           // Vector(1, 5, 7, 8, 10)
nums.sortWith(_ < _)                  // Vector(1, 5, 7, 8, 10)
nums.sortWith(_ > _)                  // Vector(10, 8, 7, 5, 1)
Vector(1,2,3).updated(0,10)           // Vector(10, 2, 3)
Vector(2,4).union(Vector(1,3))        // Vector(2, 4, 1, 3)
// zip
val women = Vector("Wilma", "Betty")  // Vector(Wilma, Betty)
val men = Vector("Fred", "Barney")    // Vector(Fred, Barney)
val couples = women.zip(men)          // Vector((Wilma,Fred), (Betty,Barney))
val a = Vector.range('a', 'e')        // Vector(a, b, c, d)
a.zipWithIndex                        // Vector((a,0), (b,1), (c,2), (d,3)) 
 MORE: Informational and mathematical methods
These methods let you obtain information from a collection.
| Method | Returns | 
|---|---|
| contains(e) | True if the vector contains the element e | 
| containsSlice(s) | True if the vector contains the sequence s | 
| count(p) | The number of elements in the vector for which the predicate is true | 
| endsWith(s) | True if the vector ends with the sequence s | 
| exists(p) | True if the predicate returns true for at least one element in the vector | 
| find(p) | The first element that matches the predicate p, returned as anOption | 
| forall(p) | True if the predicate pis true for all elements in the vector | 
| hasDefiniteSize | True if the vector has a finite size | 
| indexOf(e) | The index of the first occurrence of the element ein the vector | 
| indexOf(e,i) | The index of the first occurrence of the element ein the vector, searching only from the value of the start indexi | 
| indexOfSlice(s) | The index of the first occurrence of the sequence sin the vector | 
| indexOfSlice(s,i) | The index of the first occurrence of the sequence sin the vector, searching only from the value of the start indexi | 
| indexWhere(p) | The index of the first element where the predicate preturns true | 
| indexWhere(p,i) | The index of the first element where the predicate preturns true, searching only from the value of the start indexi | 
| isDefinedAt(i) | True if the vector contains the index i | 
| isEmpty | True if the vector contains no elements | 
| lastIndexOf(e) | The index of the last occurrence of the element ein the vector | 
| lastIndexOf(e,i) | The index of the last occurrence of the element ein the vector, occurring before or at the indexi | 
| lastIndexOfSlice(s) | The index of the last occurrence of the sequence sin the vector | 
| lastIndexOfSlice(s,i) | The index of the last occurrence of the sequence sin the vector, occurring before or at the indexi | 
| lastIndexWhere(p) | The index of the first element where the predicate preturns true | 
| lastIndexWhere(p,i) | The index of the first element where the predicate preturns true, occurring before or at the indexi | 
| max | The largest element in the vector | 
| min | The smallest element in the vector | 
| nonEmpty | True if the vector is not empty (i.e., if it contains 1 or more elements) | 
| product | The result of multiplying the elements in the collection | 
| segmentLength(p,i) | The length of the longest segment for which the predicate pis true, starting at the indexi | 
| size | The number of elements in the vector | 
| startsWith(s) | True if the vector begins with the elements in the sequence s | 
| startsWith(s,i) | True if the vector has the sequence sstarting at the indexi | 
| sum | The sum of the elements in the vector | 
| fold(s)(o) | “Fold” the elements of the vector using the binary operator o, using an initial seeds(see alsoreduce) | 
| foldLeft(s)(o) | “Fold” the elements of the vector using the binary operator o, using an initial seeds, going from left to right (see alsoreduceLeft) | 
| foldRight(s)(o) | “Fold” the elements of the vector using the binary operator o, using an initial seeds, going from right to left (see alsoreduceRight) | 
| reduce | “Reduce” the elements of the vector using the binary operator o | 
| reduceLeft | “Reduce” the elements of the vector using the binary operator o, going from left to right | 
| reduceRight | “Reduce” the elements of the vector using the binary operator o, going from right to left | 
Examples
First, some sample data:
val evens = Vector(2, 4, 6)
val odds = Vector(1, 3, 5)
val fbb = "foo bar baz"
val firstTen = (1 to 10).toVector          // Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val fiveToFifteen = (5 to 15).toVector     // Vector(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
val empty = Vector[Int]()                  // Vector[Int] = Vector()
val letters = ('a' to 'f').toVector        // Vector(a, b, c, d, e, f) 
 The examples:
evens.contains(2)                          // true
firstTen.containsSlice(Seq(3,4,5))         // true
firstTen.count(_ % 2 == 0)                 // 5
firstTen.endsWith(Seq(9,10))               // true
firstTen.exists(_ > 10)                    // false
firstTen.find(_ > 2)                       // Some(3)
firstTen.forall(_ < 20)                    // true
firstTen.hasDefiniteSize                   // true
empty.hasDefiniteSize                      // true
letters.indexOf('b')                       // 1 (zero-based)
letters.indexOf('d', 2)                    // 3
letters.indexOf('d', 3)                    // 3
letters.indexOf('d', 4)                    // -1
letters.indexOfSlice(Seq('c','d'))         // 2
letters.indexOfSlice(Seq('c','d'),2)       // 2
letters.indexOfSlice(Seq('c','d'),3)       // -1
firstTen.indexWhere(_ == 3)                // 2
firstTen.indexWhere(_ == 3, 2)             // 2
firstTen.indexWhere(_ == 3, 5)             // -1
letters.isDefinedAt(1)                     // true
letters.isDefinedAt(20)                    // false
letters.isEmpty                            // false
empty.isEmpty                              // true
// lastIndex...
val fbb = "foo bar baz"
fbb.indexOf('a')                           // 5
fbb.lastIndexOf('a')                       // 9
fbb.lastIndexOf('a', 10)                   // 9
fbb.lastIndexOf('a', 9)                    // 9
fbb.lastIndexOf('a', 6)                    // 5
fbb.lastIndexOf('a', 5)                    // 5
fbb.lastIndexOf('a', 4)                    // -1
fbb.lastIndexOfSlice("ar")                 // 5
fbb.lastIndexOfSlice(Seq('a','r'))         // 5
fbb.lastIndexOfSlice(Seq('a','r'), 4)      // -1
fbb.lastIndexOfSlice(Seq('a','r'), 5)      // 5
fbb.lastIndexOfSlice(Seq('a','r'), 6)      // 5
fbb.lastIndexWhere(_ == 'a')               // 9
fbb.lastIndexWhere(_ == 'a', 4)            // -1
fbb.lastIndexWhere(_ == 'a', 5)            // 5
fbb.lastIndexWhere(_ == 'a', 6)            // 5
fbb.lastIndexWhere(_ == 'a', 8)            // 5
fbb.lastIndexWhere(_ == 'a', 9)            // 9
firstTen.max                               // 10
letters.max                                // f
firstTen.min                               // 1
letters.min                                // a
letters.nonEmpty                           // true
empty.nonEmpty                             // false
firstTen.product                           // 3628800
letters.size                               // 6
val x = Vector(1,2,9,1,1,1,1,4)
x.segmentLength(_ < 4, 0)                  // 2
x.segmentLength(_ < 4, 2)                  // 0
x.segmentLength(_ < 4, 3)                  // 4
x.segmentLength(_ < 4, 4)                  // 3
firstTen.startsWith(Seq(1,2))              // true
firstTen.startsWith(Seq(1,2), 0)           // true
firstTen.startsWith(Seq(1,2), 1)           // false
firstTen.sum                               // 55
firstTen.fold(100)(_ + _)                  // 155
firstTen.foldLeft(100)(_ + _)              // 155
firstTen.foldRight(100)(_ + _)             // 155
firstTen.reduce(_ + _)                     // 55
firstTen.reduceLeft(_ + _)                 // 55
firstTen.reduceRight(_ + _)                // 55
firstTen.fold(100)(_ - _)                  // 45
firstTen.foldLeft(100)(_ - _)              // 45
firstTen.foldRight(100)(_ - _)             // 95
firstTen.reduce(_ - _)                     // -53
firstTen.reduceLeft(_ - _)                 // -53
firstTen.reduceRight(_ - _)                // -5 
 More on fold and reduce
MORE: Grouping methods
These methods generally let you create multiple groups from a collection.
| Method | Returns | 
|---|---|
| groupBy(f) | A map of collections created by the function f | 
| grouped | Breaks the vector into fixed-size iterable collections | 
| partition(p) | Two collections created by the predicate p | 
| sliding(i,s) | Group elements into fixed size blocks by passing a sliding window of size iand stepsover them | 
| span(p) | A collection of two collections; the first created by vector.takeWhile(p), and the second created byvector.dropWhile(p) | 
| splitAt(i) | A collection of two collections by splitting the vector at index i | 
| unzip | The opposite of zip, breaks a collection into two collections by dividing each element into two pieces; such as breaking up a vector ofTuple2elements | 
Examples
val firstTen = (1 to 10).toVector      // Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
firstTen.groupBy(_ > 5)                // Map(false -> Vector(1, 2, 3, 4, 5), true -> Vector(6, 7, 8, 9, 10))
firstTen.grouped(2)                    // Iterator[Vector[Int]] = non-empty iterator
firstTen.grouped(2).toVector           // Vector(Vector(1, 2), Vector(3, 4), Vector(5, 6), Vector(7, 8), Vector(9, 10))
firstTen.grouped(5).toVector           // Vector(Vector(1, 2, 3, 4, 5), Vector(6, 7, 8, 9, 10))
"foo bar baz".partition(_ < 'c')       // (" ba ba", foorz)  // a Tuple2
firstTen.partition(_ > 5)              // (Vector(6, 7, 8, 9, 10), Vector(1, 2, 3, 4, 5))
firstTen.sliding(2)                    // Iterator[Vector[Int]] = non-empty iterator
firstTen.sliding(2).toVector           // Vector(Vector(1, 2), Vector(2, 3), Vector(3, 4), Vector(4, 5), Vector(5, 6), Vector(6, 7), Vector(7, 8), Vector(8, 9), Vector(9, 10))  
firstTen.sliding(2,2).toVector         // Vector(Vector(1, 2), Vector(3, 4), Vector(5, 6), Vector(7, 8), Vector(9, 10))  
firstTen.sliding(2,3).toVector         // Vector(Vector(1, 2), Vector(4, 5), Vector(7, 8), Vector(10))
firstTen.sliding(2,4).toVector         // Vector(Vector(1, 2), Vector(5, 6), Vector(9, 10)) 
val x = Vector(15, 10, 5, 8, 20, 12)
x.groupBy(_ > 10)                      // Map(false -> Vector(10, 5, 8), true -> Vector(15, 20, 12))
x.partition(_ > 10)                    // (Vector(15, 20, 12), Vector(10, 5, 8))
x.span(_ < 20)                         // (Vector(15, 10, 5, 8), Vector(20, 12))
x.splitAt(2)                           // (Vector(15, 10), Vector(5, 8, 20, 12)) 
 More information:
Looping over a Vector with for and foreach
 
 These examples show how to loop/iterate over a vector with for and foreach. (As I write in Functional Programming, Simplified, foreach is only used for side effects, and therefore I rarely use it.)
val oneToFive = Vector(1, 2, 3, 4, 5)
for (i <- oneToFive) yield i           // Vector(1, 2, 3, 4, 5)
for (i <- oneToFive) yield i * 2       // Vector(2, 4, 6, 8, 10)
for (i <- oneToFive) yield i % 2       // Vector(1, 0, 1, 0, 1)
for {                                  // Vector(3, 4, 5)
    i <- oneToFive
    if i > 2
} yield i
for {                                  // Vector(6, 8, 10)
    i <- oneToFive
    if i > 2
} yield {
    // could be multiple lines here
    i * 2
}
// foreach (which i rarely use)
val oneToThree = Vector(1, 2, 3)
oneToThree.foreach(print)              // 123
for (i <- oneToThree) print(i)         // 123 
 A few things you can do with a Vector of Options
The Option type is used a lot in idiomatic Scala code, so here are some ways to work with a Vector that contains Options.
val x = Vector(Some(1), None, Some(3), None)
x.flatten                              // Vector(1, 3)
x.collect{case Some(i) => i}           // Vector(1, 3)
// map, flatten, flatMap
import scala.util.Try
def toInt(s: String): Option[Int] = Try(Integer.parseInt(s)).toOption
val strings = Vector("1", "2", "foo", "3", "bar")
strings.map(toInt)                     // Vector(Some(1), Some(2), None, Some(3), None)
strings.map(toInt).flatten             // Vector(1, 2, 3)
strings.flatMap(toInt)                 // Vector(1, 2, 3) 
 | this post is sponsored by my books: | |||
|   #1 New Release |   FP Best Seller |   Learn Scala 3 |   Learn FP Fast | 
Scala Vector summary
I hope these Vector examples are helpful. Most of these examples will work very similarly with other immutable sequential collections classes like Seq, IndexedSeq, LinearSeq, List, and more. (Eventually I’ll create dedicated pages for each of those types, and others, such as ArrayBuffer and ListBuffer.)
As a final note, if I made a mistake, or you know another way to do something with an Vector I haven’t shown, leave a note in the Comments section.






