Updated April 14, 2023
Introduction to Scala Set
Set is a part of the collection and it is used to store elements. Just like Java it also does not contain duplicate elements in it. In scala there are two types of sets are available i.e. immutable and mutable. Scala by default provides us immutable sets. But we can make our set mutable just by importing some packages. Set is used for strong and retrieving of data and it contains only unique elements if we try to add a duplicate element, it will discard those objects from it.
Syntax:
var variableName : Set[DataType] = Set()
In the above syntax, we can just provide the data type and name of our set. Let’s take a practice syntax for better understanding;
var set1 : Set[String] = Set()
In this case, we are defining an empty set here with no value initialized but we can also initialize our set at the time of declaring itself see below;
var set1 = Set("val1", "val2", "val3", "and so on ...")
In this case we are assigning value to set at the time of the creation of set only. So we can create a set in two ways which are explained above.
How Does Set Work in Scala?
Scala sets are also used to store objects and to retrieve them. It uses == to check the duplicate elements in it as it contains the only unique element. In scala sets are iterable class and part of the collection. Let’s have a look for its subclass and structure;
Scala set to contain some supertype which are mentioned below;
Equals | SetOps | Iterable | IterableFactoryDefaults | IterableOps |
IterableOnceOps | Set | IterableOnce | AnyRef | Any |
It also contains some Subclasses which are mentioned below;
ValueSet | AbstarcteSet | KeySet | BitSet | SortedSet |
KeySortedSet | HashSet | Node | ListSet | Set1 |
Set2 | Set3 | Set4 | Set | TreeSet |
LinkedKeySet | ImmutableKeySortedSet | BitSet1 | BitSet2 | BitsetN |
Now we can a look for scala set hierarchy;
Set includes the following:
- Iterable
- Equals
- SetOps
- IterableFactory
- IterableOnceExtensionMethods
- AbstarctSet
- ImmutableSet
- MutableSet
Types of Members
Set has two types of members in it:
- Abstract members
- Concrete members
some important points that we should keep in mind while working with sets in scala:
1) Scala provides us immutable seta by default, but we can also create mutable sets by importing Scala.collection.mutable._ package.
2) In scala, we can create empty sets as well as we can also initialize them at the time of creation only.
3) It provides us various methods to work with it like List in scala some of the methods are: contains(), size(), bitset(), remove, etc.
4) Set only contains unique elements.
Examples to Implement Scala Set
Below are the examples:
Example #1 – for()
In this example, we are printing the values of different types of sets.
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val set1: Set[Int] = Set(100, 200, 00, 400, 600, 900)
val set2 = Set("hello", "this is ", "string set", "bye")
// print set1
println("values in set1 are :: "+ set1)
// print set2
println("values in set2 are :: " )
for(setval<-set2)
{
println("value is ::" + setval)
}
}
Output:
Example #2 – contains()
This method checks elements in the set. Return true and false based on results.
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val set1: Set[Int] = Set(100, 200, 00, 400, 600, 900)
val set2 = Set("hello", "this is ", "string set", "bye")
// print set1
println("values in set1 are :: "+ set1)
// print set2
println("values in set2 are :: " )
var result = set1.contains(400)
println("result for set is :: "+ result)
}
Output:
Example #3 – + ()
This method always returns a new set with all the previous element present in it.
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val butset1 :BitSet = BitSet(100, 200, 00, 400, 600, 900)
println("elemets inside bit set are ::")
// bitSet+() to add more elemets
val bs1: BitSet = butset1 + 500 + 700
//bit set output
println("result for bitset is :: "+ bs1)
}
Output:
Example #4 – & ()
This method returns a new set that contains all the elements which are present in both of the sets.
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val bit1: BitSet = BitSet(1, 4, 7 , 9, 10 , 3)
val bit2: BitSet = BitSet(3, 15, 20 , 40, 7 , 1)
// Applying BitSet&() function
val result: BitSet = bit1 & (bit2)
// Bit Set & output
println("result after & method is ::: " + result)
}
Output:
Example #5 – &~ ()
This method returns a new set that contains all the elements that are not present in both sets.
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val bit1: BitSet = BitSet(1, 4, 7 , 9, 10 , 3)
val bit2: BitSet = BitSet(3, 15, 20 , 40, 7 , 1)
// Applying BitSet&() function
val result: BitSet = bit1 &~ (bit2)
// Bit Set &~ output
println("result after &~ method is ::: " + result)
}
Output:
Example #6 – – ()
This method will return a new set but does not contain the elements specify with this operator.
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val bit1: BitSet = BitSet(1, 4, 7 , 9, 10 , 3)
val bit2: BitSet = BitSet(3, 15, 20 , 40, 7 , 1)
println("before :: "+ bit1)
// Applying BitSet&() function
val result: BitSet = bit1 - 10
// Bit Set - output
println("result after-~ method is ::: " + result)
}
Output:
Example #7 – ++ ()
This method returns a new set that contains all the elements from both sets but not repeated once.
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val bit1: BitSet = BitSet(1, 4, 7 , 9, 10 , 3)
val bit2: BitSet = BitSet(3, 15, 20 , 40, 7 , 1)
println("before :: "+ bit1)
// Applying BitSet&() function
val result: BitSet = bit1 ++ bit2
// Bit Set ++ output
println("result after ++ method is ::: " + result)
}
Output:
Conclusion
Sets are used for storing and retrieving of our data. In scala, we have two types of sets that are immutable and mutable. If you want to store unique elements and want to remove the duplicity, then we should go for sets in scala.
Recommended Articles
This is a guide to Scala Set. Here we discuss a brief overview of Scala Set and its different examples along with code Implementation. You can also go through our other suggested articles to learn more –