How to remove elements from Set in Scala

56 total views, 1 views today

Welcome to Scala series. In this tutorial, you will learn about how to remove elements from 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 remove elements from 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

Remove elements from Scala

In this example, we have created a Set and removed a few elements from the Set.

object SetTest {

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

Output

5
6
3

Remove another Set from Scala Set

In this example, we are removing a set from a set.

object SetTest {

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

Output

5
1
9
7
3

Mutable Set Example

Remove elements from Scala Set

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

import scala.collection.mutable.Set

object SetTest {

  def main(args: Array[String]): Unit = {

    var set = Set(1, 2, 3, 4, 5, 6)
    set.-=(1)
    set.-=(2, 3)
    set -= 4

    set.foreach(println)
  }
}

Output

5
6

Remove another Set from Scala Set

In this example, we have explicitly imported a mutable Set and we are removing a set from a set.

import scala.collection.mutable.Set

object SetTest {

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

    set1.foreach(println)
  }
}

Output

9
1
5
3
7

References: Scala Set official documentation

I hope you like this tutorial.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.