Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Set

In Scala, Set is a collection that contains no duplicate elements and has no defined order. It provides various methods for creating, transforming, querying, and manipulating sets. Here’s a comprehensive overview of the most common functions available for Set in Scala:

Creating Sets

  • apply: Creates a set from the given elements.

    val set = Set(1, 2, 3)
    
  • empty: Creates an empty set.

    val emptySet = Set.empty[Int]
    

Basic Operations

  • contains: Checks if the set contains a specific element.

    val containsTwo = set.contains(2) // true
    
  • isEmpty: Checks if the set is empty.

    val empty = set.isEmpty // false
    
  • size: Returns the size of the set.

    val size = set.size // 3
    
  • add: Adds an element to the set (returns a new set, since sets are immutable).

    val newSet = set + 4 // Set(1, 2, 3, 4)
    
  • remove: Removes an element from the set (returns a new set).

    val smallerSet = set - 2 // Set(1, 3)
    

Transformations

  • map: Transforms each element by applying a function.

    val mapped = set.map(_ * 2) // Set(2, 4, 6)
    
  • flatMap: Transforms each element using a function and flattens the result.

    val flatMapped = set.flatMap(x => Set(x, x * 2)) // Set(1, 2, 3, 4, 6)
    
  • collect: Transforms elements using a partial function.

    val collected = set.collect { case x if x % 2 == 0 => x * 2 } // Set(4)
    

Filtering

  • filter: Selects elements that satisfy a predicate.

    val filtered = set.filter(_ % 2 == 0) // Set(2)
    
  • filterNot: Selects elements that do not satisfy a predicate.

    val filterNot = set.filterNot(_ % 2 == 0) // Set(1, 3)
    
  • partition: Splits the set into two sets based on a predicate.

    val (evens, odds) = set.partition(_ % 2 == 0)
    println(evens) // Set(2)
    println(odds)  // Set(1, 3)
    

Aggregation

  • reduce: Combines elements using a binary function.

    val sum = set.reduce(_ + _) // 6
    
  • fold: Combines elements using a binary function and an initial value.

    val sum = set.fold(0)(_ + _) // 6
    

Combining Sets

  • union: Returns the union of two sets.

    val otherSet = Set(3, 4, 5)
    val unionSet = set.union(otherSet) // Set(1, 2, 3, 4, 5)
    // or
    val unionSet2 = set | otherSet // Set(1, 2, 3, 4, 5)
    
  • intersect: Returns the intersection of two sets.

    val intersectSet = set.intersect(otherSet) // Set(3)
    // or
    val intersectSet2 = set & otherSet // Set(3)
    
  • diff: Returns the difference of two sets.

    val diffSet = set.diff(otherSet) // Set(1, 2)
    // or
    val diffSet2 = set &~ otherSet // Set(1, 2)
    

Subset and Superset

  • subsetOf: Checks if one set is a subset of another.

    val isSubset = Set(1, 2).subsetOf(set) // true
    
  • supersetOf: Checks if one set is a superset of another.

    val isSuperset = set.supersetOf(Set(1, 2)) // true
    

Conversion

  • toList: Converts the set to a list.

    val list = set.toList // List(1, 2, 3)
    
  • toArray: Converts the set to an array.

    val array = set.toArray // Array(1, 2, 3)
    
  • toSeq: Converts the set to a sequence.

    val seq = set.toSeq // Seq(1, 2, 3)
    
  • toMap: Converts a set of key-value pairs to a map.

    val pairSet = Set((1, "one"), (2, "two"))
    val map = pairSet.toMap // Map(1 -> "one", 2 -> "two")
    

Example Usage

Here are some example usages of the above functions:

val set = Set(1, 2, 3)

// Basic Operations
val containsTwo = set.contains(2) // true
val empty = set.isEmpty // false
val size = set.size // 3

// Transformations
val mapped = set.map(_ * 2) // Set(2, 4, 6)
val flatMapped = set.flatMap(x => Set(x, x * 2)) // Set(1, 2, 3, 4, 6)

// Filtering
val filtered = set.filter(_ % 2 == 0) // Set(2)

// Aggregation
val sum = set.reduce(_ + _) // 6

// Combining Sets
val otherSet = Set(3, 4, 5)
val unionSet = set.union(otherSet) // Set(1, 2, 3, 4, 5)
val intersectSet = set.intersect(otherSet) // Set(3)
val diffSet = set.diff(otherSet) // Set(1, 2)

// Subset and Superset
val isSubset = Set(1, 2).subsetOf(set) // true
val isSuperset = set.supersetOf(Set(1, 2)) // true

// Conversion
val list = set.toList // List(1, 2, 3)
val array = set.toArray // Array(1, 2, 3)
val seq = set.toSeq // Seq(1, 2, 3)
val pairSet = Set((1, "one"), (2, "two"))
val map = pairSet.toMap // Map(1 -> "one", 2 -> "two")

// Advanced Functions
val combs = set.subsets(2).toList // List(Set(1, 2), Set(1, 3), Set(2, 3))
val allSubsets = set.subsets().toList // List(Set(), Set(1), Set(2), Set(3), Set(1, 2), Set(1, 3), Set(2, 3), Set(1, 2, 3))