在 Scala Stack class
,則使用intersect()方法返回一個新堆棧,該堆棧由兩個給定堆棧中都存在的元素組成。
函數定義: def intersect[B >: A](that: collection.Seq[B]): Stack[A]
返回類型: It returns a new stack that consists of elements that are present in both the given stacks.
範例1:
// Scala program of intersect()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stacks
val s1 = Stack(1, 3, 2, 7, 6, 5)
val s2 = Stack(1, 13, 2, 17, 6, 15)
// Print the stack
println("Stack_1: " + s1)
println("Stack_2: " + s2)
// Applying intersect method
val result = s1.intersect(s2)
// Display output
print("The elements in both the stacks: ")
result.foreach(x => print(x + " "))
}
}
輸出:
Stack_1: Stack(1, 3, 2, 7, 6, 5) Stack_2: Stack(1, 13, 2, 17, 6, 15) The elements in both the stacks: 1 2 6
範例2:
// Scala program of intersect()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stacks
val s1 = Stack(1, 3, 2, 7, 6, 5)
val s2 = Stack(11, 3, 12, 7, 16, 5)
// Print the stack
println("Stack_1: " + s1)
println("Stack_2: " + s2)
// Applying intersect method
val result = s1.intersect(s2)
// Display output
print("The elements in both the stacks: " + result)
}
}
輸出:
Stack_1: Stack(1, 3, 2, 7, 6, 5) Stack_2: Stack(11, 3, 12, 7, 16, 5) The elements in both the stacks: Stack(3, 7, 5)
相關用法
- Scala Set intersect()用法及代碼示例
- Scala TreeSet intersect()用法及代碼示例
- Scala Queue intersect()用法及代碼示例
- Scala SortedSet intersect()用法及代碼示例
- Scala Stack map()用法及代碼示例
- Scala Stack :()用法及代碼示例
- Scala Stack max()用法及代碼示例
- Scala Stack min()用法及代碼示例
- Scala Stack contains()用法及代碼示例
- Scala Stack top()用法及代碼示例
- Scala Stack sum()用法及代碼示例
- Scala Stack +:()用法及代碼示例
- Scala Stack ++:()用法及代碼示例
- Scala Stack last()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Scala Stack intersect() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。