Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
A HashSet
is a collection of unique elements, and Scala provides both mutable and immutable versions of it. Unlike List
or Array
, a HashSet
does not maintain elements in any specific order. It provides constant-time average performance for the basic operations (add
, contains
, and remove
).
By default, when you refer to a HashSet
in Scala, you're using the immutable version. Once an immutable HashSet
is created, it cannot be changed. Any operation that seems to "modify" it actually returns a new HashSet
.
import scala.collection.immutable.HashSet val set = HashSet(1, 2, 3, 4)
Remember, these operations don't modify the original set but return a new one:
val newSet = set + 5 // HashSet(1, 2, 3, 4, 5) val reducedSet = set - 3 // HashSet(1, 2, 4)
If you need to modify a HashSet
in place, you'd use the mutable version from the scala.collection.mutable
package.
import scala.collection.mutable.HashSet val mutableSet = HashSet(1, 2, 3)
You can modify the set in-place:
mutableSet += 4 // Adds 4 to the set mutableSet -= 2 // Removes 2 from the set
Check if an element exists: mutableSet.contains(3)
Clearing all elements: mutableSet.clear()
Union of two sets:
val set1 = HashSet(1, 2, 3) val set2 = HashSet(3, 4, 5) val unionSet = set1 union set2 // Alternatively: set1 | set2
Intersection of two sets:
val intersectSet = set1 intersect set2 // Alternatively: set1 & set2
Difference between two sets:
val diffSet = set1 diff set2 // Elements in set1 but not in set2
Iterating over a HashSet:
for (element <- set) { println(element) }
Size of the HashSet: set.size
The choice between mutable and immutable HashSet
depends on your use case. While the immutable version is typically preferred in a functional programming context because of its safety in concurrent scenarios and predictability, there are situations where the mutable version may be more performant or appropriate.
Creating and initializing HashSet in Scala:
HashSet
companion object or through mutable and immutable variations.// Immutable HashSet val immutableSet = Set(1, 2, 3) // Mutable HashSet import scala.collection.mutable.HashSet val mutableSet = HashSet("a", "b", "c")
Mutable and immutable HashSet in Scala:
// Immutable HashSet val immutableSet = Set(1, 2, 3) // Mutable HashSet import scala.collection.mutable.HashSet val mutableSet = HashSet("a", "b", "c")
HashSet operations and methods in Scala:
+
, -
, ++
, --
, and more for modification.val set = Set(1, 2, 3) val updatedSet = set + 4
Adding and removing elements from HashSet in Scala:
+
and removed using -
operations.val set = Set(1, 2, 3) val updatedSet = set + 4 val removedSet = updatedSet - 2
Accessing values in Scala HashSet:
val set = Set(1, 2, 3) val containsTwo = set.contains(2)
Iterating over HashSet in Scala:
foreach
, for
, or other iteration methods.val set = Set(1, 2, 3) set.foreach { value => println(s"Value: $value") }
Updating values in Scala HashSet:
+
operation with an existing value.val set = Set(1, 2, 3) val updatedSet = set + 2 // Updating value to 2
Scala HashSet vs. other collection types:
val list = List(1, 2, 3) val map = Map("a" -> 1, "b" -> 2)
Pattern matching with HashSet in Scala:
val set = Set(1, 2, 3) set.foreach { case value if value % 2 == 0 => println(s"Even value: $value") case _ => // Handle other cases }
Default values and options in Scala HashSet:
contains
with Option
can handle non-existing values in HashSets.val set = Set(1, 2, 3) val containsFour = set.contains(4) // Check for existence val valueOption = set.find(_ == 2) // Using Option
Scala HashSet with case classes:
case class Person(name: String, age: Int) val personSet = Set(Person("Alice", 25), Person("Bob", 30))