Article directory
In Scala, Set is a collection type used to store a set of unique elements. Scala provides two types: mutable Set and immutable Set.
1. Variable Set
A mutable Set has the ability to add, remove, and update elements, and the set can be modified in place. The classes for mutable collections are located scala.collection.mutable
under the package.
Example using a mutable Set:
import scala.collection.mutable.Set
// 创建可变 Set
val mutableSet = Set(1, 2, 3)
// 添加元素
mutableSet.add(4)
mutableSet += 5
// 删除元素
mutableSet.remove(3)
mutableSet -= 2
// 输出可变 Set
println(mutableSet) // 输出:Set(1, 4, 5)
2. Immutable Set
Immutable Sets are immutable; once they are created, they cannot be modified. To add, remove or update elements, a new Set is returned. The classes for immutable collections are located scala.collection.immutable
under the package.
Example using immutable Set:
// 创建不可变 Set
val immutableSet = Set(1, 2, 3)
// 添加元素(会返回一个新的 Set)
val newImmutableSet = immutableSet + 4
// 删除元素(会返回一个新的 Set)
val newerImmutableSet = newImmutableSet - 3
// 输出原始不可变 Set 和新的 Set
println(immutableSet) // 输出:Set(1, 2, 3)
println(newerImmutableSet) // 输出:Set(1, 2, 4)
Note: In Scala, it is highly recommended to use immutable collection types because of their better thread safety and functional programming features. Mutable collection types should only be used in specific scenarios where the collection needs to be modified in place. Immutable collections are safer in concurrent environments and easier to reason about and test, making them an important supporting tool for functional programming.
3. Characteristics of Set
In addition to mutable and immutable Sets, Scala's Sets have some other important features and uses. Let's take a look at them:
-
Set properties:
- A Set is a collection in which duplicate elements are not allowed. Each element can only appear once in the Set.
- Sets are unordered, with no definite order between elements.
- Sets are not indexable, and elements cannot be accessed via index.
-
Creation of Set:
- Create a Set using
Set
the companion object's method:apply
val mySet: Set[Int] = Set(1, 2, 3)
Set
Create a Set using the factory method of :
val mySet: Set[Int] = Set.empty[Int] val anotherSet: Set[Int] = Set(4, 5, 6)
- Create a Set using
-
Common operations of Set:
+
: Add elements to Set and return new Set.-
: Remove elements from Set and return a new Set.++
: Merge two Sets and return a new Set.--
: Remove elements from another Set from Set and return a new Set.contains
: Check whether the Set contains the specified element.size
: Get the size of Set (number of elements).
-
Conversion of Set:
- Use
map
the method to map the elements of the Set. - Use
filter
the method to filter elements that meet the criteria. - Use
flatMap
the method to flatten the elements in the Set.
- Use
-
Intersection, union and difference of Sets:
- Use
intersect
the method to calculate the intersection of two Sets. - Use
union
the method to calculate the union of two Sets. - Use
diff
the method to calculate the difference between two Sets.
- Use
val set1: Set[Int] = Set(1, 2, 3)
val set2: Set[Int] = Set(3, 4, 5)
val intersection: Set[Int] = set1.intersect(set2) // 交集:Set(3)
val union: Set[Int] = set1.union(set2) // 并集:Set(1, 2, 3, 4, 5)
val difference: Set[Int] = set1.diff(set2) // 差集:Set(1, 2)
- Traversal of Set:
- Use
foreach
the method to iterate through each element in the Set.
- Use
val mySet: Set[Int] = Set(1, 2, 3)
mySet.foreach {
element =>
println(element)
}
- Unique operations for variable Sets:
- Mutable Set supports in-place modification, and you can use
+=
the and-=
operators to add and delete elements.
- Mutable Set supports in-place modification, and you can use
import scala.collection.mutable.Set
val mutableSet = Set(1, 2, 3)
mutableSet += 4
mutableSet -= 2
println(mutableSet) // 输出:Set(1, 3, 4)
Summary: Set is a data type in Scala that represents a set, where duplicate elements are not allowed. Scala provides two types of Sets, immutable and mutable. Immutable Sets are more suitable for functional programming and multi-threaded environments. Set supports common set operations, including addition, deletion, conversion, traversal, etc. In addition, Set also supports common set operations such as calculating intersection, union and difference.