How to add elements to Set in Scala

 135 total views,  4 views today

Welcome to Scala series. In this tutorial, you will learn about how to add elements to Set in Scala.

Scala set is a collection that stores only unique values. It doesn’t allow duplicates. Set is Iterable and inherits all functionality of the iterable interface. Check out this detailed post on Scala Set for various Set operations.

Let us now understand how to add elements to Scala Set.

Basically Set in Scala has two variants.

  • Immutable Set(Which is the default)
  • Mutable Set(we need to explicitly import mutable Set)

Immutable Set allows us to apply a function on Set but resulting in the creation of a new Set every time. It doesn’t allow us to change the source. On the other hand, the mutable Set allows modifying the source itself.


Immutable Set Examples

Add elements to Set

In this example, we have created a Set and added a few elements to the Set.

object SetTest {

  def main(args: Array[String]): Unit = {
    var set: Set[Int] = Set(1, 2, 3, 4, 5, 6)
    set = set.+(7)
    set = set.+(8, 9)
    set.foreach(println)
  }
}

Output

5
1
6
9
2
7
3
8
4

Add a Set to another Set

In this example, we are adding a Set to another Set.

object SetTest {

  def main(args: Array[String]): Unit = {
    
    var set1: Set[Int] = Set(1, 3, 5, 7, 9)
    var set2: Set[Int] = Set(2, 4, 6, 8)
    var set3 = set1.++(set2)
    
    set3.foreach(println)
  }
}

Output

5
1
6
9
2
7
3
8
4

Mutable Set Example

Add elements to Set

In this example, we have explicitly imported a mutable Set and added elements.

import scala.collection.mutable.Set

object SetTest {

  def main(args: Array[String]): Unit = {
    var set = Set(1, 2, 3, 4, 5, 6)
    set.+=(7)
    set.+=(8, 9)
    set += 10
    set.foreach(println)
  }
}

 

Output

9
1
5
2
6
3
10
7
4
8

Add another Set to Scala Set

In this example, we have explicitly imported a mutable Set and added another Set.

import scala.collection.mutable.Set

object SetTest {

  def main(args: Array[String]): Unit = {
    var set1 = Set(1, 3, 5, 7, 9)
    var set2 = Set(2, 4, 6, 8)
    set1.++=(set2)
    set1.foreach(println)
  }
}

Output

9
1
5
2
6
3
7
4
8

References: Scala Set official documentation

I hope you like this tutorial and you were able to understand how to add elements to Set in Scala.

Thanks for reading and please give us a thumb up and comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.