Scala 集合 - Set



Scala Set 是一個由相同型別且互不相同的元素組成的集合。換句話說,Set 是一個不包含重複元素的集合。Set 有兩種型別:**不可變**和**可變**。不可變物件和可變物件的區別在於,不可變物件本身不能被修改。

預設情況下,Scala 使用不可變 Set。如果要使用可變 Set,則必須顯式匯入 **scala.collection.mutable.Set** 類。如果要在同一個集合中同時使用可變和不可變 Set,則可以繼續將不可變 Set 稱為 **Set**,而可變 Set 稱為 **mutable.Set**。

以下是宣告不可變 Set 的方法:

語法

// Empty set of integer type
var s : Set[Int] = Set()
// Set of integer type
var s : Set[Int] = Set(1,3,5,7)

or 

var s = Set(1,3,5,7)

定義空集合時,型別註解是必要的,因為系統需要為變數分配一個具體的型別。

Set 的基本操作

Set 的所有操作都可以用以下三種方法來表達:

序號 方法及描述
1

head

此方法返回集合的第一個元素。

2

tail

此方法返回包含除第一個元素之外所有元素的集合。

3

isEmpty

如果集合為空,則此方法返回 true,否則返回 false。

嘗試以下示例,其中顯示了基本操作方法的使用:

示例

object Demo {
   def main(args: Array[String]) {
      val fruit = Set("apples", "oranges", "pears")
      val nums: Set[Int] = Set()
      println( "Head of fruit : " + fruit.head )
      println( "Tail of fruit : " + fruit.tail )
      println( "Check if fruit is empty : " + fruit.isEmpty )
      println( "Check if nums is empty : " + nums.isEmpty )
   }
}

將上述程式儲存為 **Demo.scala**。使用以下命令編譯並執行此程式。

命令

\>scalac Demo.scala
\>scala Demo

輸出

Head of fruit : apples
Tail of fruit : Set(oranges, pears)
Check if fruit is empty : false
Check if nums is empty : true

連線集合

可以使用 **++** 運算子或 **Set.++()** 方法來連線兩個或多個集合,但在新增集合時會刪除重複元素。

以下是連線兩個集合的示例。

示例

object Demo {
   def main(args: Array[String]) {
      val fruit1 = Set("apples", "oranges", "pears")
      val fruit2 = Set("mangoes", "banana")
      // use two or more sets with ++ as operator
      var fruit = fruit1 ++ fruit2
      println( "fruit1 ++ fruit2 : " + fruit )
      // use two sets with ++ as method
      fruit = fruit1.++(fruit2)
      println( "fruit1.++(fruit2) : " + fruit )
   }
}

將上述程式儲存為 **Demo.scala**。使用以下命令編譯並執行此程式。

命令

\>scalac Demo.scala
\>scala Demo

輸出

fruit1 ++ fruit2 : Set(banana, apples, mangoes, pears, oranges)
fruit1.++(fruit2) : Set(banana, apples, mangoes, pears, oranges)

查詢集合中的最大值和最小值元素

可以使用 **Set.min** 方法查詢集合中元素的最小值,使用 **Set.max** 方法查詢集合中元素的最大值。以下示例顯示了程式。

示例

object Demo {
   def main(args: Array[String]) {
      val num = Set(5,6,9,20,30,45)
      // find min and max of the elements
      println( "Min element in Set(5,6,9,20,30,45) : " + num.min )
      println( "Max element in Set(5,6,9,20,30,45) : " + num.max )
   }
}

將上述程式儲存為 **Demo.scala**。使用以下命令編譯並執行此程式。

命令

\>scalac Demo.scala
\>scala Demo

輸出

Min element in Set(5,6,9,20,30,45) : 5
Max element in Set(5,6,9,20,30,45) : 45

查詢集合中的公共值

可以使用 **Set.&** 方法或 **Set.intersect** 方法查詢兩個集合之間的公共值。嘗試以下示例以顯示用法。

示例

object Demo {
   def main(args: Array[String]) {
      val num1 = Set(5,6,9,20,30,45)
      val num2 = Set(50,60,9,20,35,55)
      // find common elements between two sets
      println( "num1.&(num2) : " + num1.&(num2) )
      println( "num1.intersect(num2) : " + num1.intersect(num2) )
   }
}

將上述程式儲存為 **Demo.scala**。使用以下命令編譯並執行此程式。

命令

\>scalac Demo.scala
\>scala Demo

輸出

num1.&(num2) : Set(20, 9)
num1.intersect(num2) : Set(20, 9)
廣告
© . All rights reserved.