[Scala Collection] 16. Set

Insert image description here

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.mutableunder 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.immutableunder 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:

  1. 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.
  2. Creation of Set:

    • Create a Set using Setthe companion object's method:apply
    val mySet: Set[Int] = Set(1, 2, 3)
    
    • SetCreate a Set using the factory method of :
    val mySet: Set[Int] = Set.empty[Int]
    val anotherSet: Set[Int] = Set(4, 5, 6)
    
  3. 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).
  4. Conversion of Set:

    • Use mapthe method to map the elements of the Set.
    • Use filterthe method to filter elements that meet the criteria.
    • Use flatMapthe method to flatten the elements in the Set.
  5. Intersection, union and difference of Sets:

    • Use intersectthe method to calculate the intersection of two Sets.
    • Use unionthe method to calculate the union of two Sets.
    • Use diffthe method to calculate the difference between two Sets.
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)
  1. Traversal of Set:
    • Use foreachthe method to iterate through each element in the Set.
val mySet: Set[Int] = Set(1, 2, 3)

mySet.foreach {
    
     element =>
  println(element)
}
  1. Unique operations for variable Sets:
    • Mutable Set supports in-place modification, and you can use +=the and -=operators to add and delete elements.
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.

Guess you like

Origin blog.csdn.net/m0_47256162/article/details/132159803