Related Articles
Scala Queue diff() method with example
• Last Updated : 23 Oct, 2019

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
Recommended Articles
Page :