collections.png

Set,与Seq, Map并列

Set是没有重复元素的Iterable,方法分为如下几类:

  • Test 

    方法:containsapplysubsetOf

   contains方法询问是否set包含了yield被给与的元素.

   apply同contains,即 set(elem) 等同于 set contains elem

For example

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
  • Additions

    方法:+ and ++

   增加一个或者多个元素

  • Removals

    方法: - , --

   删除一个或者多个元素

  • Set operations

  方法:union, intersection, diff

   这些方法有两种形式,字母和字符。

   

intersect&
union|
diff&~

 继承自Traversable的++方法有点类似unoin。(后面略,没意义)


Class Set 操作

WHAT IT ISWHAT IT DOES
Tests:
xs contains x测试 xs是否包含x
xs(x)同上
xs subsetOf ys测试xs是否是ys的子集
Additions:
xs + xSet xs 添加元素x ,返回Set(子类可能重写)
xs + (x, y, z)Set xs 添加括号中的元素 返回Set
xs ++ ysxs ys合并后的所有元素 返回 Set
Removals

xs - xSet 包含xs中除去x的所有元素
xs - (x, y, z)Set 包含xs出去括号中元素的所有元素
xs -- ysSet 包含xs中除去ys中元素的所有元素
xs.empty清空xs
Binary Operations:
xs & ysxs和ys的交集
xs intersect ys
xs | ysxs和ys的并集
xs union ys
xs &~ ysxs和ys的区别
xs diff ys