Home »
Scala
HashSet in Scala
By IncludeHelp Last updated : November 14, 2024
Scala HashSet
A HashSet is a special type of collection that is unordered i.e. the order of inserted elements is not defined. For storing its elements and keeping track of them it uses hashmaps.
In Scala programming language, HashSet class extents immutable set along with abstractSet trait. The element in HashSet is stored using hashcode.
Syntax
var hashset_name = HashSet(elements...)
Now, as we know the basics of HashSet in scala lets create one and perform some operations on it.
Creating a HashSet in Scala
Creating a HashSet in the Scala programming language is quite an easy process. The below code shows how to,
Program to show the implementation creation of hashset
import scala.collection.immutable.HashSet
object myObject {
def main(args: Array[String]): Unit = {
val hashSet: HashSet[Int] = HashSet(23, 53, 89, 1, 90)
println(s"The elements of the HashSet are = $hashSet")
}
}
Output
The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
Finding an element from HashSet in Scala
Searching for an element from HashSet in Scala is a direct process. HashSet in scala will return a boolean value (True or False) for element found or not.
Program to show the implementation element search in hashset
import scala.collection.immutable.HashSet
object myObject {
def main(args: Array[String]): Unit = {
val hashSet: HashSet[Int] = HashSet(23, 53, 89, 1, 90)
println(s"The elements of the HashSet are = $hashSet")
println("Number 43 is in HashSet? " + hashSet.contains(43))
println("Number 90 is in HashSet? " + hashSet.contains(90))
}
}
Output
The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
Number 43 is in HashSet? false
Number 90 is in HashSet? true
Adding elements in HashSet
a) Adding Single element
In a HashSet, addition operation is also valid. The + operator is used to add the new element in scala HashSet. As HashSet is an immutable addition will be implemented in a new HashSet.
Program to show the implementation of adding elements in hashset
import scala.collection.immutable.HashSet
object myObject {
def main(args: Array[String]): Unit = {
val hashSet: HashSet[Int] = HashSet(23, 53, 89, 1, 90)
println(s"The elements of the HashSet are = $hashSet")
val hashSet2: HashSet[Int] = hashSet + 45
println(s"The elements of new HashSet are = $hashSet2")
}
}
Output
The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
The elements of new HashSet are = HashSet(53, 45, 23, 90, 89, 1)
Adding multiple elements in HashSet
The ++ operator is used to add more than one element in HashSet.
Program to show the implementation of adding elements in hashset
import scala.collection.immutable.HashSet
object myObject {
def main(args: Array[String]): Unit = {
val hashSet: HashSet[Int] = HashSet(23, 53, 89, 1, 90)
println(s"The elements of the HashSet are = $hashSet")
val hashSet2: HashSet[Int] = hashSet ++ HashSet[Int](45, 31)
println(s"The elements of new HashSet are = $hashSet2")
}
}
Output
The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
The elements of new HashSet are = HashSet(53, 45, 31, 23, 90, 89, 1)
Removing elements from HashSet in Scala
Elements of HashSet can be removed by using the - operator in Scala. Again you need to create a new HashSet to assign the operated HashSet.
Program to show the implementation of removing elements in hashset
import scala.collection.immutable.HashSet
object myObject {
def main(args: Array[String]): Unit = {
val hashSet: HashSet[Int] = HashSet(23, 53, 89, 1, 90)
println(s"The elements of the HashSet are = $hashSet")
val hashSet2: HashSet[Int] = hashSet - 53
println(s"The elements of new HashSet are = $hashSet2")
}
}
Output
The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
The elements of new HashSet are = HashSet(23, 90, 89, 1)
Find the elements that are in common in Two HashSets (Intersection operation)
In Scala common elements between two HashSet using the & sign, this will be given common elements in both the HashSets.
Program to show the implementation of intersection of hashset
import scala.collection.immutable.HashSet
object myObject {
def main(args: Array[String]): Unit = {
val hashSet: HashSet[Int] = HashSet(23, 53, 89, 1, 90)
println(s"The elements of the HashSet1 are = $hashSet")
val hashSet2: HashSet[Int] = HashSet(5, 23, 45, 1, 31, 90)
println(s"The elements of new HashSet2 are = $hashSet2")
println("The intersection of HashSet1 and HashSet2 are " + (hashSet & hashSet2))
}
}
Output
The elements of the HashSet1 are = HashSet(53, 23, 90, 89, 1)
The elements of new HashSet2 are = HashSet(5, 1, 45, 31, 23, 90)
The intersection of HashSet1 and HashSet2 are HashSet(1, 23, 90)
Creating an Empty HashSet in Scala
In Scala, we can create an Empty HashSet with zero elements.
Example
import scala.collection.immutable.HashSet
object myObject {
def main(args: Array[String]): Unit = {
val hashSet: HashSet[Int] = HashSet.empty[Int]
println("An empty HashSet : " + hashSet)
}
}
Output
An empty HashSet : HashSet()