Scala Set (Collection)
A Scala Set is a collection of objects with no duplicates, where all elements are unique.
Scala collections are divided into mutable and immutable collections.
By default, Scala uses immutable collections. If you want to use a mutable collection, you need to import the scala.collection.mutable.Set package.
The default reference is scala.collection.immutable.Set, and an immutable collection instance is as follows:
Example
val set = Set(1,2,3)
println(set.getClass.getName) //
println(set.exists(_ % 2 == 0)) //true
println(set.drop(1)) //Set(2,3)
If you need to use a mutable collection, you need to import scala.collection.mutable.Set:
Example
import scala.collection.mutable.Set // Mutable collections can be imported anywhere
val mutableSet = Set(1,2,3)
println(mutableSet.getClass.getName) // scala.collection.mutable.HashSet
mutableSet.add(4)
mutableSet.remove(1)
mutableSet += 5
mutableSet -= 2
println(mutableSet) // Set(5, 3, 4)
val another = mutableSet.toSet
println(another.getClass.getName) // scala.collection.immutable.Set
>
Note: Although both mutable and immutable Sets have operations to add or remove elements, there is a significant difference. Operating on an immutable Set will produce a new set, and the original set will not change, which is similar to a List. However, operating on a mutable Set will change the Set itself, similar to a ListBuffer.
Basic Operations of Collections
Scala collections have three basic operations:
head
returns the first element of the collection.tail
returns a collection containing all elements except the first one.isEmpty
returns true when the collection is empty.
Any operation on a Scala collection can be expressed using these three basic operations. Example as follows:
Example
object Test {
def main(args: Array[String]) {
val site = Set("tutorialpro", "Google", "Baidu")
val nums: Set[Int] = Set()
println("The first website is: " + site.head)
println("The last website is: " + site.tail)
println("Check if the list site is empty: " + site.isEmpty)
println("Check if nums is empty: " + nums.isEmpty)
}
}
Executing the above code, the output will be:
$ vim Test.scala
$ scala Test.scala
The first website is: tutorialpro
The last website is: Set(Google, Baidu)
Check if the list site is empty: false
Check if nums is empty: true
Connecting Collections
You can use the ++ operator or the Set.++() method to connect two collections. If there are duplicate elements, they will be removed. Example as follows:
Example
object Test {
def main(args: Array[String]) {
val site1 = Set("tutorialpro", "Google", "Baidu")
val site2 = Set("Facebook", "Taobao")
// ++ as an operator
var site = site1 ++ site2
println("site1 ++ site2: " + site)
// ++ as a method
site = site1.++(site2)
println("site1.++(site2): " + site)
}
}
Executing the above code, the output will be:
$ vim Test.scala
$ scala Test.scala
site1 ++ site2: Set(Facebook, Taobao, Google, Baidu, tutorialpro)
site1.++(site2): Set(Facebook, Taobao, Google, Baidu, tutorialpro)
Finding the Maximum and Minimum Elements in a Collection
You can use the Set.min method to find the minimum element in a collection, and the Set.max method to find the maximum element. Example as follows:
Example
object Test {
def main(args: Array[String]) {
val num = Set(5,6,9,20,30,45)
// Find the maximum and minimum elements in the collection
println("The minimum element in Set(5,6,9,20,30,45) is: " + num.min)
println("The maximum element in Set(5,6,9,20,30,45) is: " + num.max)
}
}
Executing the above code, the output will be:
``` $ vim Test.scala $ scala Test.scala The minimum element in Set(5,6,9,20, | 35 | def product: A Returns the product of all numeric elements in the immutable set. | | 36 | def size: Int Returns the number of elements in the immutable set | | 37 | def splitAt(n: Int): (Set[A], Set[A]) Splits the immutable set into two containers, the first consisting of the first n elements, and the second consisting of the remaining elements | | 38 | def subsetOf(that: Set[A]): Boolean Returns true if the set contains a subset, otherwise returns false | | 39 | def sum: A Returns the sum of all numeric elements in the immutable set | | 40 | def tail: Set[A] Returns all elements in the immutable set except the first one | | 41 | def take(n: Int): Set[A] Returns the first n elements | | 42 | def takeRight(n: Int):Set[A] Returns the last n elements | | 43 | def toArray: Array[A] Converts the set to an array | | 44 | def toBuffer[B >: A]: Buffer[B] Returns a buffer containing all elements of the immutable set | | 45 | def toList: List[A] Returns a List containing all elements of the immutable set | | 46 | def toMap[T, U]: Map[T, U] Returns a Map containing all elements of the immutable set | | 47 | def toSeq: Seq[A] Returns a Seq containing all elements of the immutable set | | 48 | def toString(): String Returns a string representing the object |
For more methods, refer to the API documentation