A set is a collection which only contains unique items. The uniqueness of a set are defined by the == method of the type that set holds. If you try to add a duplicate item in the set, then set quietly discard your request.
Syntax:
// Immutable set val variable_name: Set[type] = Set(item1, item2, item3) or val variable_name = Set(item1, item2, item3) // Mutable Set var variable_name: Set[type] = Set(item1, item2, item3) or var variable_name = Set(item1, item2, item3)
Some Important Points about Set in Scala
- In Scala, both mutable and immutable sets are available. Mutable set is those set in which the value of the object is change but, in the immutable set, the value of the object is not changed itself.
- By default set in Scala are immutable.
- In Scala, the immutable set is defined under Scala.collection.immutable._ package and mutable set are defined under Scala.collection.mutable._ package.
- We can also define a mutable set under Scala.collection.immutable._ package as shown in the below example.
- A Set has various methods to add, remove clear, size, etc. to enhance the usage of the set.
- In Scala, We are allowed to create empty set.
Syntax:// Immutable empty set val variable_name = Set() // Mutable empty set var variable_name = Set()
Example 1:
// Scala program to illustrate the // use of immutable set import scala.collection.immutable._ object Main { def main(args: Array[String]) { // Creating and initializing immutable sets val myset1: Set[String] = Set("Geeks", "GFG", "GeeksforGeeks", "Geek123") val myset2 = Set("C", "C#", "Java", "Scala", "PHP", "Ruby") // Display the value of myset1 println("Set 1:") println(myset1) // Display the value of myset2 using for loop println("\nSet 2:") for(myset<-myset2) { println(myset) } } } |
Output:
Set 1: Set(Geeks, GFG, GeeksforGeeks, Geek123) Set 2: Scala C# Ruby PHP C Java
Example 2:
// Scala program to illustrate the // use of mutable set import scala.collection.immutable._ object Main { def main(args: Array[String]) { // Creating and initializing mutable sets var myset1: Set[String] = Set("Geeks", "GFG", "GeeksforGeeks", "Geek123") var myset2 = Set(10, 100, 1000, 10000, 100000) // Display the value of myset1 println("Set 1:") println(myset1) // Display the value of myset2 // using a foreach loop println("\nSet 2:") myset2.foreach((item:Int)=>println(item)) } } |
Output:
Set 1: Set(Geeks, GFG, GeeksforGeeks, Geek123) Set 2: 10 100000 10000 1000 100
Example 3:
// Scala program to illustrate the // use of empty set import scala.collection.immutable._ object Main { def main(args: Array[String]) { // Creating empty sets val myset = Set() // Display the value of myset println("The empty set is:") println(myset) } } |
Output:
The empty set is: Set()
Sorted Set
In Set, SortedSet is used to get values from the set in sorted order. SortedSet is only work for immutable set.
Example:
// Scala program to get sorted values // from the set import scala.collection.immutable.SortedSet object Main { def main(args: Array[String]) { // Using SortedSet to get sorted values val myset: SortedSet[Int] = SortedSet(87, 0, 3, 45, 7, 56, 8,6) myset.foreach((items: Int)=> println(items)) } } |
Output:
0 3 6 7 8 45 56 87
Recommended Posts:
- Scala Tutorial – Learn Scala with Step By Step Guide
- Set in Scala | Set-2
- Conversion from a Java Set to a Scala Set
- Program to print Java Set of characters in Scala
- Scala Set &() method with example
- Program to convert Java set of Shorts to an Indexed Sequence in Scala
- Scala Set dropRight() method with example
- Scala Set equals() method with example
- Scala Set drop() method with example
- Scala Set dropWhile() method with example
- Scala Set diff() method with example
- Scala Set copyToArray() method with example
- Scala Set subsetOf() method with example
- Program to convert Java Set to a String in Scala
- Scala Trait Traversable | Set-1
- Scala Trait Traversable | Set-2
- Scala Trait Traversable | Set-3
- Scala Trait Traversable | Set-4
- Scala | Methods to Call on a Map | Set-1
- Program to convert Java Set of doubles to a String in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

