Scala Set intersect() 方法及示例
intersect() 方法用于计算两个集合之间的交集。
方法定义:def intersect(that: Set[A]): Set[A]
返回类型:返回一个包含两个集合中共同元素的集合。
例子#1:
//Scala程序使用intersect()方法
//创建对象
object GfG
{
//主方法
def main(args:Array[String])
{
//创建集合
val s1 = Set(1, 2, 3, 4, 5)
val s2 = Set(11, 12, 13, 4, 5)
//应用intersect方法
val s3 = s1.intersect(s2)
s3.foreach(x => println(x))
}
}
5
4
例子#2:
//Scala程序使用intersect()方法
//创建对象
object GfG
{
//主方法
def main(args:Array[String])
{
//创建集合
val s1 = Set(1, 2, 3, 4, 5)
val s2 = Set(11, 2, 3, 4, 5)
//应用intersect方法
val s3 = s1.intersect(s2)
s3.foreach(x => println(x))
}
}
5
2
3
4
阅读更多:Scala 教程
极客教程