Scala Vector filtering methods (examples)

This page contains a collection of examples of filtering methods that can be used with a Scala Vector class. I’ll first show a table that describes the filtering methods, and then show examples of them.

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 n elements
dropRight(n) Return all elements except the last n elements
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 Vector is 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 Vector is empty
lastOption The last element as an Option
slice(f,u) A sequence of elements from index f (from) to index u (until)
tail All elements after the first element
take(n) The first n elements
takeRight(n) The last n elements
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 information

I created this tutorial to show only Scala Vector filtering methods, but for many more examples, see my Scala Vector class syntax and method examples tutorial.