首页 > 代码库 > Scala Set的一些用法

Scala Set的一些用法

scala> val set = Set(3,2,4,8,1)
set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 8, 4)

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)

scala> set ++ set1
res66: scala.collection.immutable.Set[Int] = Set(1, 9, 2, 3, 8, 4)

scala> set -- set1
res67: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)

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)

scala> set.slice(1,4)
res83: scala.collection.immutable.Set[Int] = Set(2, 3, 8)

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)



Scala Set的一些用法