scala> val set = Set(3,2,4,8,1) set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 8, 4) //取set里面最小值 scala> set.min res58: Int = 1 //取最大值 scala> set.max res59: Int = 8 //元素个数 scala> set.size res60: Int = 5 scala> val set1 = Set(9,8) set1: scala.collection.immutable.Set[Int] = Set(9, 8) //交集 scala> set & set1 res63: scala.collection.immutable.Set[Int] = Set(8) //交集 scala> set.intersect(set1) res65: scala.collection.immutable.Set[Int] = Set(8) //连接两个set scala> set ++ set1 res66: scala.collection.immutable.Set[Int] = Set(1, 9, 2, 3, 8, 4) //在第一个set基础上去掉第二个set中存在的元素, 返回 scala> set -- set1 res67: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4) //返回第一个不同于第二个set的元素集合 scala> set &~ set1 res68: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4) //计算符合条件的元素个数 scala> set1.count(_ > 5) res70: Int = 2 //返回第一个不同于第二个的元素集合 scala> set.diff(set1) res71: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4) scala> set1.diff(set) res72: scala.collection.immutable.Set[Int] = Set(9) //取子set(1,4为元素位置, 从0开始) scala> set.slice(1,4) res83: scala.collection.immutable.Set[Int] = Set(2, 3, 8) //迭代所有的子set scala> set.subsets(2).foreach(x=>println(x)) Set(1, 2) Set(1, 3) Set(1, 8) Set(1, 4) Set(2, 3) Set(2, 8) Set(2, 4) Set(3, 8) Set(3, 4) Set(8, 4)