Scala Queue diff() method with example

The diff() method is used to find the difference between the two queues. It deletes elements that are present in one queue from the other one.

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.

Example #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)
    


Output:

Queue_1: Queue(1, 2, 3, 4, 5)
Queue_2: Queue(3, 4, 5)
(Queue_1 - Queue_2): Queue(1, 2)

Example #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)
    


Output:

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)