Home » Scala language

HashSet in Scala

In this tutorial, we will learn about HashSet in Scala with its operations and working. We will see working examples and operations that are valid on HashSet in Scala.
Submitted by Shivang Yadav, on December 05, 2019

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,

1) Program to show the implementation creation of hashset

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 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.

2) Program to show the implementation element search in hashset

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 90) 
		println(s"The elements of the HashSet are = $hashSet") 
		println("Number 43 is in HashSet? "+ {hashSet(43)})
		println("Number 90 is in HashSet? "+ {hashSet(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]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 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)

b) 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]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 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.

1) program to show the implementation of removing elements in hashset

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 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]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 90) 
		println(s"The elements of the HashSet1 are = $hashSet") 
		val hashSet2 : HashSet[Int] = HashSet(5, 23, 45, 01, 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.

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet.empty[Int]
		println("An empty HashSet : "+ hashSet)
	} 
} 

Output

An empty HashSet : HashSet()





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.