scala> val s1 = Set(1,2,3,4) s1: scala.collection.Set[Int] = Set(1, 2, 3, 4) scala> val s2 = Set(3,4,5,6) s2: scala.collection.Set[Int] = Set(3, 4, 5, 6)
交集
scala> s1 & s2 res78: scala.collection.Set[Int] = Set(3, 4) scala> s1 intersect s2 res79: scala.collection.Set[Int] = Set(3, 4)
并集
scala> s1 | s2 res80: scala.collection.Set[Int] = Set(5, 1, 6, 2, 3, 4) scala> s1 union s2 res81: scala.collection.Set[Int] = Set(5, 1, 6, 2, 3, 4)
scala> s1++ s2 res91: scala.collection.Set[Int] = Set(5, 1, 6, 2, 3, 4)
差集
scala> s1 -- s2 res86: scala.collection.Set[Int] = Set(1, 2) scala> s1 &~ s2 res89: scala.collection.Set[Int] = Set(1, 2) scala> s1 diff s2 res90: scala.collection.Set[Int] = Set(1, 2)