📜  Scala Queue intersect() 方法与示例

📅  最后修改于: 2022-05-13 01:54:28.169000             🧑  作者: Mango

Scala Queue intersect() 方法与示例

intersect()方法用于返回一个新队列,该队列由两个给定队列中存在的元素组成。

示例 #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 q1 = Queue(1, 3, 2, 7, 6, 5) 
          
        val q2 = Queue(1, 13, 2, 17, 6, 15) 
          
        // Print the queues
        println("Queue_1: " + q1)
          
        println("Queue_2: " + q2)
          
        // Applying intersect method 
        val result = q1.intersect(q2)
          
        // Display output
        print("The elements in both the queues: ")
          
        result.foreach(x => print(x + " "))
          
    } 
} 
输出:
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

示例 #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 q1 = Queue(1, 3, 2, 7, 6, 5) 
          
        val q2 = Queue(11, 3, 12, 7, 16, 5) 
          
        // Print the queues
        println("Queue_1: " + q1)
          
        println("Queue_2: " + q2)
          
        // Applying intersect method 
        val result = q1.intersect(q2)
          
        // Display output
        print("The elements in both the queues: " + result)
          
    } 
} 
输出:
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)