This is an excerpt from the 1st Edition of the Scala Cookbook (partially modified for the internet). This is Recipe 11.26, “How to Add Elements to a Set in Scala”
Problem
You want to add elements to a mutable set, or create a new set by adding elements to an immutable set.
Solution
Mutable and immutable sets are handled differently, as demonstrated in the following examples.
Mutable set
Add elements to a mutable Set
with the +=
, ++=
, and add
methods:
// use var with mutable scala> val set = scala.collection.mutable.Set[Int]() set: scala.collection.mutable.Set[Int] = Set() // add one element scala> set += 1 res0: scala.collection.mutable.Set[Int] = Set(1) // add multiple elements scala> set += (2, 3) res1: scala.collection.mutable.Set[Int] = Set(2, 1, 3) // notice that there is no error when you add a duplicate element scala> set += 2 res2: scala.collection.mutable.Set[Int] = Set(2, 6, 1, 4, 3, 5) // add elements from any sequence (any TraversableOnce) scala> set ++= Vector(4, 5) res3: scala.collection.mutable.Set[Int] = Set(2, 1, 4, 3, 5) scala> set.add(6) res4: Boolean = true scala> set.add(5) res5: Boolean = false
The last two examples demonstrate a unique characteristic of the add
method on a set: It returns true
or false
depending on whether or not the element was added to the set. The other methods silently fail if you attempt to add an element that’s already in the set.
You can test to see whether a set contains an element before adding it:
set.contains(5)
But as a practical matter, I use +=
and ++=
, and ignore whether the element was already in the set.
Whereas the first example demonstrated how to create an empty set, you can also add elements to a mutable set when you declare it, just like other collections:
scala> val set = scala.collection.mutable.Set(1, 2, 3) set: scala.collection.mutable.Set[Int] = Set(2, 1, 3)
Immutable Set in Scala
The following examples show how to create a new immutable set by adding elements to an existing immutable Scala Set
.
First, create an immutable Set
:
scala> val s1 = Set(1, 2) s1: scala.collection.immutable.Set[Int] = Set(1, 2)
Create a new set by adding elements to a previous set with the + and ++ methods:
// add one element scala> val s2 = s1 + 3 s2: scala.collection.immutable.Set[Int] = Set(1, 2, 3) // add multiple elements (+ method has a varargs field) scala> val s3 = s2 + (4, 5) s3: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4) // add elements from another sequence scala> val s4 = s3 ++ List(6, 7) s4: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 7, 3, 4)
I showed these examples with immutable variables just to be clear about how the approach works. You can also declare your variable as a var
, and reassign the resulting set back to the same variable:
scala> var set = Set(1, 2, 3) set: scala.collection.immutable.Set[Int] = Set(1, 2, 3) scala> set += 4 scala> set res0: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
See Recipe 10.6, “Understanding Mutable Variables with Immutable Collections” for more information on the difference between mutable/immutable variables and mutable/immutable collections.
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |