HashSet In Scala - GeeksforGeeks

04 Jul, 2019
HashSet is sealed class. It extends immutable Set and AbstractSet trait. Hash code is used to store elements. It neither sorts the elements nor maintains insertion order . The Set interface implemented by the HashSet class, backed by a hash table . In Scala, A concrete implementation of Set semantics is known HashSet.Syntax:
var HashsetName = HashSet(element1, element2, element3, ....)  
Initialize a HashSet : Below is the example to create or initialize HashSet.Example :// Scala program of Initializing HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hashSet: HashSet[String] = HashSet("Geeks",                                    "GeeksForGeeks", "Author")        println(s"Elements are = $hashSet")    }} Output:Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
// Scala program of Initializing HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hashSet: HashSet[String] = HashSet("Geeks",                                    "GeeksForGeeks", "Author")        println(s"Elements are = $hashSet")    }} 
Output:
Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
Check specific elements in HashSet :Example :// Scala program of Check specific elements in HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hashSet: HashSet[String] = HashSet("Geeks",                                "GeeksForGeeks", "Author")        println(s"Elements are = $hashSet")                  // Checking        println(s"Element Geeks = ${hashSet("Geeks")}")        println(s"Element Student = ${hashSet("Student")}")    }} Output:Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
Element Geeks = true
Element Student = false

// Scala program of Check specific elements in HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hashSet: HashSet[String] = HashSet("Geeks",                                "GeeksForGeeks", "Author")        println(s"Elements are = $hashSet")                  // Checking        println(s"Element Geeks = ${hashSet("Geeks")}")        println(s"Element Student = ${hashSet("Student")}")    }} 
Output:
Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
Element Geeks = true
Element Student = false

Adding an elements in HashSet : We can add an element in HashSet by using + sign. below is the example of adding an element in HashSet.Example :// Scala program of adding an element in HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hs: HashSet[String] = HashSet("Geeks",                            "GeeksForGeeks", "Author")        println(s"Elements are = $hs")                  // Adding an element in HashSet        val hs1: HashSet[String] = hs + "GeeksClasses"        println(s"Adding elements to HashSet = $hs1")    }}Output:Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
Adding elements to HashSet = Set(GeeksClasses, Geeks, Author, GeeksForGeeks)

// Scala program of adding an element in HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hs: HashSet[String] = HashSet("Geeks",                            "GeeksForGeeks", "Author")        println(s"Elements are = $hs")                  // Adding an element in HashSet        val hs1: HashSet[String] = hs + "GeeksClasses"        println(s"Adding elements to HashSet = $hs1")    }}
Output:
Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
Adding elements to HashSet = Set(GeeksClasses, Geeks, Author, GeeksForGeeks)

Adding more than one element in HashSet : We can add more than one element in HashSet by using ++ sign. below is the example of adding more than one elements in HashSet.Example :// Scala program of adding more elements in HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hs: HashSet[String] = HashSet("Geeks",                                "GeeksForGeeks", "Author")        println(s"Elements are = $hs")                  // Adding elements in HashSet        val hs1: HashSet[String] = hs ++ HashSet[String]("Java", "Scala")        println(s"Add more than one HashSets = $hs1")    }}Output:Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
Add more than one HashSets = Set(Scala, Geeks, Author, Java, GeeksForGeeks)

// Scala program of adding more elements in HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hs: HashSet[String] = HashSet("Geeks",                                "GeeksForGeeks", "Author")        println(s"Elements are = $hs")                  // Adding elements in HashSet        val hs1: HashSet[String] = hs ++ HashSet[String]("Java", "Scala")        println(s"Add more than one HashSets = $hs1")    }}
Output:
Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
Add more than one HashSets = Set(Scala, Geeks, Author, Java, GeeksForGeeks)

Remove element in HashSet : We can remove an element in HashSet by using – sign. below is the example of removing an element in HashSet.Example :// Scala program of removing element in HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hs: HashSet[String] = HashSet("Geeks",                                "GeeksForGeeks", "Author")        println(s"Elements are = $hs")                  // removing elements in HashSet        val hs1: HashSet[String] = hs - "Geeks"        println(s"remove element from hashset = $hs1")    }}Output:Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
remove element from hashset = Set(Author, GeeksForGeeks)
// Scala program of removing element in HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize a HashSet")                  // Creating HashSet        val hs: HashSet[String] = HashSet("Geeks",                                "GeeksForGeeks", "Author")        println(s"Elements are = $hs")                  // removing elements in HashSet        val hs1: HashSet[String] = hs - "Geeks"        println(s"remove element from hashset = $hs1")    }}
Output:
Initialize a HashSet
Elements are = Set(Geeks, Author, GeeksForGeeks)
remove element from hashset = Set(Author, GeeksForGeeks)
Find the intersection between two HashSets : We can find intersection between two HashSets by using & sign. below is the example of finding intersection between two HashSets.Example :// Scala program of finding the intersection between two HashSetsimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize two HashSets")                  // Creating two HashSet        val hs: HashSet[String] = HashSet("Geeks",                            "GeeksForGeeks", "Author")        println(s"Elements of hashset1 are = $hs")                  val hs1: HashSet[String] = HashSet("Java",                                     "Geeks", "Scala")        println(s"Elements of hashset2 are = $hs1")                  // finding the intersection between two HashSets        println(s"Intersection of hashSet1 and hashSet2 = ${hs & hs1}")    }}Output:Initialize two HashSets
Elements of hashset1  are = Set(Geeks, Author, GeeksForGeeks)
Elements of hashset2 are = Set(Scala, Geeks, Java)
Intersection of hashSet1 and hashSet2 = Set(Geeks)

// Scala program of finding the intersection between two HashSetsimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         println("Initialize two HashSets")                  // Creating two HashSet        val hs: HashSet[String] = HashSet("Geeks",                            "GeeksForGeeks", "Author")        println(s"Elements of hashset1 are = $hs")                  val hs1: HashSet[String] = HashSet("Java",                                     "Geeks", "Scala")        println(s"Elements of hashset2 are = $hs1")                  // finding the intersection between two HashSets        println(s"Intersection of hashSet1 and hashSet2 = ${hs & hs1}")    }}
Output:
Initialize two HashSets
Elements of hashset1  are = Set(Geeks, Author, GeeksForGeeks)
Elements of hashset2 are = Set(Scala, Geeks, Java)
Intersection of hashSet1 and hashSet2 = Set(Geeks)

Initializing an empty HashSet :Example :// Scala program of  Initializing an empty HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         // Initializing an empty HashSet        val emptyHashSet: HashSet[String] = HashSet.empty[String]        println(s"Empty HashSet = $emptyHashSet")    }}Output:Empty HashSet = Set()
// Scala program of  Initializing an empty HashSetimport scala.collection.immutable.HashSet  // Creating objectobject GFG{     // Main method    def main(args:Array[String])    {         // Initializing an empty HashSet        val emptyHashSet: HashSet[String] = HashSet.empty[String]        println(s"Empty HashSet = $emptyHashSet")    }}
Output:
Empty HashSet = Set()
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
For Loop in Scala
Scala | flatMap Method
Scala | map() method
Scala List filter() method with example
Scala | reduce() Function
String concatenation in Scala
Type Casting in Scala
Scala Tutorial – Learn Scala with Step By Step Guide
Scala List contains() method with example
Scala String substring() method with example