intersect()方法用於計算兩個集合之間的交集。
函數定義: def intersect(that: Set[A]): Set[A]
返回類型: It returns a set containing the elements present in both the sets.
範例1:
// Scala program of intersect()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(1, 2, 3, 4, 5)
val s2 = Set(11, 12, 13, 4, 5)
// Applying intersect method
val s3 = s1.intersect(s2)
s3.foreach(x => println(x))
}
}
輸出:
5 4
範例2:
// Scala program of intersect()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(1, 2, 3, 4, 5)
val s2 = Set(11, 2, 3, 4, 5)
// Applying intersect method
val s3 = s1.intersect(s2)
s3.foreach(x => println(x))
}
}
輸出:
5 2 3 4
相關用法
- Scala TreeSet intersect()用法及代碼示例
- Scala SortedSet intersect()用法及代碼示例
- Scala Stack intersect()用法及代碼示例
- Scala Queue intersect()用法及代碼示例
- Scala Map max()用法及代碼示例
- Scala Int max()用法及代碼示例
- Scala Int min()用法及代碼示例
- Scala Map min()用法及代碼示例
- Scala Int self()用法及代碼示例
- Scala Set sum()用法及代碼示例
- Scala Map get()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Scala Set intersect() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。