# 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` `q``1` `=` `Queue(``1``, ``2``, ``3``, ``4``, ``5``)  ` `         `  `        ``val` `q``2` `=` `Queue(``3``, ``4``, ``5``)  ` `         `  `        ``// Print the queue ` `        ``println(``"Queue_1: "` `+ q``1``) ` `         `  `        ``println(``"Queue_2: "` `+ q``2``) ` `         `  `        ``// Applying diff method  ` `        ``val` `result ``=` `q``1``.diff(q``2``)  ` `         `  `        ``// 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` `q``1` `=` `Queue(``1``, ``2``, ``3``, ``4``, ``5``)  ` `         `  `        ``val` `q``2` `=` `Queue(``3``, ``4``, ``5``, ``6``, ``7``, ``8``)  ` `         `  `        ``// Print the queue ` `        ``println(``"Queue_1: "` `+ q``1``) ` `         `  `        ``println(``"Queue_2: "` `+ q``2``) ` `         `  `        ``// Applying diff method  ` `        ``val` `result ``=` `q``2``.diff(q``1``)  ` `         `  `        ``// 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)
```

My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.