Scala Set operations explained

61 total views, 1 views today

Welcome to Scala series. In this tutorial, you will learn about Scala Set and I have explained most of the Set operations in detail.

  1. Set is a collection to store values that don’t have/allow duplicate values. Always Set will have unique elements.
  2. Set is Iterable and inherits all functionality of the iterable interface.
  3. There are two variants of Set in Scala
    • Immutable Set(default)
    • Mutable Set
  4. By default, Scala will use Immutable Set. In any case, if a mutable Set is needed, then we need to explicitly import mutable Set.
  5. 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.
  6. For more information on Scala immutable and mutable collection, please visit here.

 

Let’s get started in learning Set operations and how to use them.

1. Create a Set

var set: Set[Int] = Set(1, 2, 3, 4) //option 1

var set = Set(1, 2, 3, 4) //option 2

2. Create an empty Set

var set: Set[Int] = Set()

var set = Set()

3. Get size of the Set

var set: Set[Int] = Set(1, 2, 3, 4, 5, 6)

println(set.size) //Output is 6

4. Access each element of a Set

The set is not an ordered collection. Hence we cannot access the element using the index. There is no such thing called index in Set.

So to access the elements of Set, we have to iterate over the elements using foreach.


5. Iterate the Set

var set: Set[Int] = Set(1, 2, 3, 4, 5, 6)

set.foreach(println)

Output
5
1
6
2
3
4

6. Get Head element from the Set

var set: Set[Int] = Set(1, 2, 3, 4, 5, 6)

println(set.head)

7. Get tail element from the Set

var set: Set[Int] = Set(1, 2, 3, 4, 5, 6)

println(set.tail)

8. Add Elements to a 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


9. Add 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


10. Remove element 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


11. Remove a Set from another 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


12. Find the Min

var set: Set[Int] = Set(1, 2, 3, 4, 5, 6, 7, 8, 9)

print(set.min)

Output
1

13. Find the Max

var set: Set[Int] = Set(1, 2, 3, 4, 5, 6, 7, 8, 9)

print(set.max)

Output
9

14. Find the Count

object SetTest {

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

    var set: Set[Int] = Set(1, 2, 3, 4, 5, 6, 7, 8, 9)

    println(set.count(ele => ele < 6))
  }
}

Output
5

15. Find the Sum

var set: Set[Int] = Set(1, 2, 3, 4, 5, 6, 7, 8, 9)

println(set.sum)

Output
45

References: Scala Set official documentation

I hope you like this tutorial and you were able to understand about Scala Set and its various operations.

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.