Scala Queue diff() 方法与示例
diff()方法用于查找两个队列之间的差异。它从另一个队列中删除存在于一个队列中的元素。
Method Definition: def diff[B >: A](that: collection.Seq[B]): Queue[A]
Return Type: It returns a new queue which consists of elements after the difference between the two queues.
示例 #1:
// Scala program of diff()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating queues
val q1 = Queue(1, 2, 3, 4, 5)
val q2 = Queue(3, 4, 5)
// Print the queue
println("Queue_1: " + q1)
println("Queue_2: " + q2)
// Applying diff method
val result = q1.diff(q2)
// Displays output
print("(Queue_1 - Queue_2): " + result)
}
}
输出:
Queue_1: Queue(1, 2, 3, 4, 5)
Queue_2: Queue(3, 4, 5)
(Queue_1 - Queue_2): Queue(1, 2)
示例 #2:
// Scala program of diff()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating queues
val q1 = Queue(1, 2, 3, 4, 5)
val q2 = Queue(3, 4, 5, 6, 7, 8)
// Print the queue
println("Queue_1: " + q1)
println("Queue_2: " + q2)
// Applying diff method
val result = q2.diff(q1)
// Displays output
print("(Queue_2 - Queue_1): " + result)
}
}
输出:
Queue_1: Queue(1, 2, 3, 4, 5)
Queue_2: Queue(3, 4, 5, 6, 7, 8)
(Queue_2 - Queue_1): Queue(6, 7, 8)