Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, a Set
is a collection that contains no duplicate elements. It's a part of the standard library and can be found under scala.collection.immutable.Set
for the immutable version and scala.collection.mutable.Set
for the mutable version. By default, when you use Set
, you're using the immutable version, which means its contents cannot be changed after it's created. If you need a Set
that can be updated, you would use the mutable version.
Here's a breakdown and usage of both:
Immutable sets are those whose contents cannot be changed after they're created. This means you can't add or remove elements from an immutable set, but you can create a new set based on an existing one.
import scala.collection.immutable.Set val set1 = Set(1, 2, 3, 4, 5) val set2 = set1 + 6 // Returns a new set with '6' added val set3 = set2 - 3 // Returns a new set with '3' removed println(set1) // Set(1, 2, 3, 4, 5) println(set2) // Set(1, 2, 3, 4, 5, 6) println(set3) // Set(1, 2, 4, 5, 6)
Mutable sets allow for elements to be added or removed after the set is created. They offer methods like +=
for adding and -=
for removing.
import scala.collection.mutable.Set val set = Set(1, 2, 3, 4, 5) set += 6 // Adds '6' to the set set -= 3 // Removes '3' from the set println(set) // Set(1, 2, 4, 5, 6)
Here are some common operations you can perform with sets:
Adding an Element:
val newSet = set + element
set += element
Removing an Element:
val newSet = set - element
set -= element
Union of Two Sets: set1 union set2
or set1 ++ set2
Intersection of Two Sets: set1 intersect set2
or set1 & set2
Difference of Two Sets: set1 diff set2
or set1 -- set2
Checking for Existence: set.contains(element)
Size of the Set: set.size
No Duplicates: Sets ensure that no duplicate values are stored.
Ordering: The default Set
implementation (both mutable and immutable) does not guarantee order. If you need order, you can use scala.collection.immutable.SortedSet
or scala.collection.mutable.LinkedHashSet
.
Performance: The basic operations (add
, remove
, contains
) are typically efficient, but performance can vary based on the specific Set
implementation.
In practice, the choice between mutable and immutable sets in Scala often depends on the specific use case and the desire for functional programming paradigms. Immutable collections are favored in functional programming due to their predictability and safety in concurrent scenarios.
Creating and initializing Sets in Scala:
Set
keyword and initialized with elements using curly braces.// Creating and initializing Sets val set1 = Set(1, 2, 3) val set2 = Set("apple", "orange", "banana")
Operations on Sets in Scala:
val set1 = Set(1, 2, 3) val set2 = Set(2, 3, 4) val unionResult = set1 union set2 val intersectResult = set1 intersect set2 val diffResult = set1 diff set2
Mutable vs Immutable Sets in Scala:
// Immutable Set val immutableSet = Set(1, 2, 3) // Mutable Set val mutableSet = scala.collection.mutable.Set(1, 2, 3) mutableSet.add(4)
Common Set methods in Scala:
contains
, subsetOf
, isEmpty
, and more for common set operations.val set = Set(1, 2, 3) val containsTwo = set.contains(2) val isSubset = Set(1, 2).subsetOf(set) val isEmptySet = set.isEmpty
Combining and merging Sets in Scala:
++
operator or the union
method.val set1 = Set(1, 2, 3) val set2 = Set(3, 4, 5) val combinedSet = set1 ++ set2 val mergedSet = set1 union set2
Pattern matching with Sets in Scala:
val fruitSet = Set("apple", "orange", "banana") val result = "apple" match { case x if fruitSet.contains(x) => s"$x is a fruit!" case _ => "Not a fruit" }
Set intersection and difference in Scala:
val set1 = Set(1, 2, 3) val set2 = Set(2, 3, 4) val intersection = set1 intersect set2 val difference = set1 diff set2
Set implementation details in Scala:
// Hash Set val hashSet = scala.collection.mutable.HashSet(1, 2, 3) // Tree Set val treeSet = scala.collection.immutable.TreeSet(3, 1, 2)