Scala SortedSet intersect() 方法与示例
intersect()方法用于计算两个 SortedSet 之间的交集。
Method Definition: def intersect(that: SortedSet[A]): SortedSet[A]
Return Type: It returns a SortedSet containing the elements present in both the SortedSets.
示例 #1:
// Scala program of intersect()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedSet
val s1 = SortedSet(1, 2, 3, 4, 5)
val s2 = SortedSet(11, 12, 13, 4, 5)
// Applying intersect method
val s3 = s1.intersect(s2)
s3.foreach(x => println(x))
}
}
输出:
4
5
示例 #2:
// Scala program of intersect()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedSet
val s1 = SortedSet(1, 2, 3, 4, 5)
val s2 = SortedSet(11, 2, 3, 4, 5)
// Applying intersect method
val s3 = s1.intersect(s2)
s3.foreach(x => println(x))
}
}
输出:
2
3
4
5