
Set,與Seq, Map并列
Set是沒有重複元素的Iterable,方法分為如下幾類:
Test
方法:<code>contains</code>, <code>apply</code>, <code>subsetOf</code>
<code> contains方法詢問是否set包含了yield被給與的元素.</code>
<code> apply同contains,即 set(elem) 等同于 set contains elem</code>
For example
1
2
3
4
5
6
<code>scala> </code><code>val</code> <code>fruit </code><code>=</code> <code>Set(</code><code>"apple"</code><code>, </code><code>"orange"</code><code>, </code><code>"peach"</code><code>, </code><code>"banana"</code><code>)</code>
<code>fruit</code><code>:</code> <code>scala.collection.immutable.Set[java.lang.String] </code><code>=</code> <code>Set(apple, orange, peach, banana)</code>
<code>scala> fruit(</code><code>"peach"</code><code>)</code>
<code>res</code><code>0</code><code>:</code> <code>Boolean </code><code>=</code> <code>true</code>
<code>scala> fruit(</code><code>"potato"</code><code>)</code>
<code>res</code><code>1</code><code>:</code> <code>Boolean </code><code>=</code> <code>false</code>
<code>Additions</code>
方法:<code>+</code> and <code>++</code>
<code> 增加一個或者多個元素</code>
<code>Removals</code>
<code>方法: - , --</code>
<code> 删除一個或者多個元素</code>
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
<code></code>
本文轉自 wws5201985 51CTO部落格,原文連結:http://blog.51cto.com/yjplxq/1432320,如需轉載請自行聯系原作者