QQ:515840062

# Scala：Set

Scala开篇（目录）

Set集合类型，各种语言都有，用它来存储不重复的数据元素。因为它也属于集合类型，所以它的很多操作都和Array差不多（Array（集合、序列））或者参考官方文档，所以这里就不重点讲关于set的操作，我们主要来看一下它的内部实现。

Set是trait类型，它的声明很简单

trait Set[A] extends Iterable[A]
with scala.collection.Set[A]
with GenericSetTemplate[A, Set]
with SetLike[A, Set[A]]
with Parallelizable[A, ParSet[A]]
{
override def companion: GenericCompanion[Set] = Set
override def toSet[B >: A]: Set[B] = to[({type l[a] = immutable.Set[B]})#l] // for bincompat; remove in dev
override def seq: Set[A] = this
protected override def parCombiner = ParSet.newCombiner[A] // if immutable.SetLike gets introduced, please move this there!
}

def contains(key: A): Boolean
def iterator: Iterator[A]
def +(elem: A): This
def -(elem: A): This

# Set1，Set2，Set3，Set4

    var set_1 = Set(1)
var set_2 = Set(1,2)
var set_3 = Set(1,2,3)
var set_4 = Set(1,2,3,4)
println(set_1.getClass.getName)
println(set_2.getClass.getName)
println(set_3.getClass.getName)
println(set_4.getClass.getName)
/**
scala.collection.immutable.Set$Set1 scala.collection.immutable.Set$Set2
scala.collection.immutable.Set$Set3 scala.collection.immutable.Set$Set4
*/

    var set_1 = Set(1)
set_1 = set_1+2
println(set_1.getClass.getName)
/**
scala.collection.immutable.Set\$Set2
*/

  class Set1[A] private[collection] (elem1: A) extends AbstractSet[A] with Set[A] with Serializable {
override def size: Int = 1
def contains(elem: A): Boolean =
elem == elem1
/**
通过 + 操作符，先判断是否包含要添加的元素，如果包含，直接返回自己，不会重复添加
如果不包括，因为元素增加了，所以要扩容为2个元素的set类型，所以new一个Set2类型
*/
def + (elem: A): Set[A] =
if (contains(elem)) this
else new Set2(elem1, elem)
......
}

  class Set4[A] private[collection] (elem1: A, elem2: A, elem3: A, elem4: A) extends AbstractSet[A] with Set[A] with Serializable {
override def size: Int = 4
def contains(elem: A): Boolean =
elem == elem1 || elem == elem2 || elem == elem3 || elem == elem4
def + (elem: A): Set[A] =
if (contains(elem)) this
//这里，new一个HashSet类型对象
else new HashSet[A] + (elem1, elem2, elem3, elem4, elem)
......
}

# 几个常用的操作

### 合并集合（重复数据会被忽略）

val setA = Set(1,2)
val setB = Set(3,4)
val setC = setA ++ setB
println(setC.toList.mkString(","))      /**1,2,3,4*/

//或者
val setC = setA + (3,4)
//或者
val setC = setA + 3 + 4

### 求交集（&）

val setA = Set(1,2,3,4)
val setB = Set(3,4,5)
val setC = setA & setB
println(setC.toList.mkString(","))
/**
3,4
*/

### 求一个集合中不在另一个集合中的元素（&~）

val setA = Set(1,2,3,4)
val setB = Set(3,4,5)
val setC = setA &~ setB
println(setC.toList.mkString(","))
/**
1,2
*/

### 迭代所有的子集合subsets

val setA = Set(1,2,3,4)
println(setA.subsets().toList.mkString("\n"))

/**打印结果
Set()
Set(1)
Set(2)
Set(3)
Set(4)
Set(1, 2)
Set(1, 3)
Set(1, 4)
Set(2, 3)
Set(2, 4)
Set(3, 4)
Set(1, 2, 3)
Set(1, 2, 4)
Set(1, 3, 4)
Set(2, 3, 4)
Set(1, 2, 3, 4)
*/

### 只列出指定数量元素的子集合subsets(len: Int)

val setA = Set(1,2,3,4)
println(setA.subsets(2).toList.mkString("\n"))
/**
Set(1, 2)
Set(1, 3)
Set(1, 4)
Set(2, 3)
Set(2, 4)
Set(3, 4)
*/

07-21 3.1万

10-13 2659

03-30 7946

02-19 690

08-03 1769

09-11 5581

06-06 5282

11-09 5万

10-14 3832

09-20 1248