【scala初學】collections Sets

        collections.png

Set,與Seq, Map並列
java

Set是沒有重複元素的Iterable,方法分爲以下幾類:app

  • Test ide

    方法:containsapplysubsetOf測試

   contains方法詢問是否set包含了yield被給與的元素.ui

   apply同contains,即 set(elem) 等同於 set contains elemspa

For examplescala

scala> val fruit = Set("apple", "orange", "peach", "banana")
fruit: scala.collection.immutable.Set[java.lang.String] = Set(apple, orange, peach, banana)
scala> fruit("peach")
res0: Boolean = true
scala> fruit("potato")
res1: Boolean = false
  • Additionscode

    方法:+ and ++orm

   增長一個或者多個元素繼承

  • Removals

    方法: - , --

   刪除一個或者多個元素

  • Set operations

  方法:union, intersection, diff

   這些方法有兩種形式,字母和字符。

   

intersect &
union |
diff &~

 繼承自Traversable的++方法有點相似unoin。(後面略,沒意義)


Class Set 操做

WHAT IT IS WHAT IT DOES
Tests:
xs contains x 測試 xs是否包含x
xs(x) 同上
xs subsetOf ys 測試xs是不是ys的子集
Additions:
xs + x Set xs 添加元素x ,返回Set(子類可能重寫)
xs + (x, y, z) Set xs 添加括號中的元素 返回Set
xs ++ ys xs ys合併後的全部元素 返回 Set
Removals

xs - x Set 包含xs中除去x的全部元素
xs - (x, y, z) Set 包含xs出去括號中元素的全部元素
xs -- ys Set 包含xs中除去ys中元素的全部元素
xs.empty 清空xs
Binary Operations:
xs & ys xs和ys的交集
xs intersect ys
xs | ys xs和ys的並集
xs union ys
xs &~ ys xs和ys的區別
xs diff ys
相關文章
相關標籤/搜索