Methods to “update” Scala Vector and Seq alvin May 21, 2018 - 3:18pm

This page shows a few examples of methods that can be used to update a Scala Vector. Note that these same methods will also work with a Scala Seq, including IndexedSeq.

Getting the union, intersection, and difference of Scala sets

A cool thing about Scala sets -- and some other Scala collections -- is that you can easily determine the union, intersection, and difference between two sets. The following examples demonstrate how the methods work. First, we create two sets that have a slight overlap:

scala> val low = 1 to 5 toSet
low: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> val medium = (3 to 7).toSet
medium: scala.collection.immutable.Set[Int] = Set(5, 6, 7, 3, 4)

Now we exercise the methods. First, the union: