Scala Queue intersect() 方法及示例
intersect() 方法用于返回由两个给定队列中都存在的元素组成的新队列。
方法定义:def intersect[B >: A](that: collection.Seq[B]): Queue[A]
返回类型:返回由两个给定队列中都存在的元素组成的新队列。
示例1:
// intersect() 方法的 Scala 程序
// 导入 Queue
import scala.collection.mutable._
// 创建对象
object GfG
{
// 主方法
def main(args:Array[String])
{
// 创建队列
val q1 = Queue(1, 3, 2, 7, 6, 5)
val q2 = Queue(1, 13, 2, 17, 6, 15)
// 打印队列
println("Queue_1: " + q1)
println("Queue_2: " + q2)
// 应用 intersect() 方法
val result = q1.intersect(q2)
// 显示输出
print("The elements in both the queues: ")
result.foreach(x => print(x + " "))
}
}
Queue_1: Queue(1, 3, 2, 7, 6, 5)
Queue_2: Queue(1, 13, 2, 17, 6, 15)
The elements in both the queues: 1 2 6
示例2:
// intersect() 方法的 Scala 程序
// 导入 Queue
import scala.collection.mutable._
// 创建对象
object GfG
{
// 主方法
def main(args:Array[String])
{
// 创建队列
val q1 = Queue(1, 3, 2, 7, 6, 5)
val q2 = Queue(11, 3, 12, 7, 16, 5)
// 打印队列
println("Queue_1: " + q1)
println("Queue_2: " + q2)
// 应用 intersect() 方法
val result = q1.intersect(q2)
// 显示输出
print("The elements in both the queues: " + result)
}
}
Queue_1: Queue(1, 3, 2, 7, 6, 5)
Queue_2: Queue(11, 3, 12, 7, 16, 5)
The elements in both the queues: Queue(3, 7, 5)
阅读更多:Scala 教程
极客教程