Scala Queue diff()方法及示例

Scala Queue diff()方法及示例

diff() 方法用于查找两个队列之间的差异。它将一个队列中存在的元素从另一个队列中删除。

方法定义: def diff[B >: A](that: collection.Seq[B]):Queue[A]

返回类型。它返回一个新的队列,该队列由两个队列之间的差异后的元素组成。

例子 #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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程