# Scala Queue intersect() method with example

• Last Updated : 29 Oct, 2019

The intersect() method is utilized to return a new queue that consists of elements that are present in both the given queues.

Method Definition: def intersect[B >: A](that: collection.Seq[B]): Queue[A]

Return Type: It returns a new queue that consists of elements that are present in both the given queues.

Example #1:

 `// Scala program of intersect() ``// 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``, ``3``, ``2``, ``7``, ``6``, ``5``) ``         ` `        ``val` `q``2` `=` `Queue(``1``, ``13``, ``2``, ``17``, ``6``, ``15``) ``         ` `        ``// Print the queues``        ``println(``"Queue_1: "` `+ q``1``)``         ` `        ``println(``"Queue_2: "` `+ q``2``)``         ` `        ``// Applying intersect method ``        ``val` `result ``=` `q``1``.intersect(q``2``)``         ` `        ``// Display output``        ``print(``"The elements in both the queues: "``)``         ` `        ``result.foreach(x ``=>` `print(x + ``" "``))``         ` `    ``} ``} `
Output:
```Queue_1: Queue(1, 3, 2, 7, 6, 5)
Queue_2: Queue(1, 13, 2, 17, 6, 15)
The elements in both the queues: 1 2 6
```

Example #2:

 `// Scala program of intersect() ``// 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``, ``3``, ``2``, ``7``, ``6``, ``5``) ``         ` `        ``val` `q``2` `=` `Queue(``11``, ``3``, ``12``, ``7``, ``16``, ``5``) ``         ` `        ``// Print the queues``        ``println(``"Queue_1: "` `+ q``1``)``         ` `        ``println(``"Queue_2: "` `+ q``2``)``         ` `        ``// Applying intersect method ``        ``val` `result ``=` `q``1``.intersect(q``2``)``         ` `        ``// Display output``        ``print(``"The elements in both the queues: "` `+ result)``         ` `    ``} ``} `
Output:
```Queue_1: Queue(1, 3, 2, 7, 6, 5)
Queue_2: Queue(11, 3, 12, 7, 16, 5)
The elements in both the queues: Queue(3, 7, 5)
```

My Personal Notes arrow_drop_up