Scala Queue distinct()方法及示例

Scala Queue distinct()方法及示例

利用 distinct() 方法,可以从给定的队列中移除重复的元素。

方法定义:def distinct:Queue[A]

返回类型。它返回一个只包含不同元素的新队列。

例子 #1:

// Scala program of distinct() 
// method 
  
// Import Queue 
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating queue
        val s1 = Queue(2, 1, 3, 2, 1, 2, 3) 
          
        // Print the queue
        println(s1) 
          
        // Applying distinct() method  
        val result = s1.distinct
          
        // Display output 
        print("Queue with distinct elements: " + result) 
           
    } 
} 

输出。

Queue(2, 1, 3, 2, 1, 2, 3)
Queue with distinct elements: Queue(2, 1, 3)

例子#2。

// Scala program of distinct() 
// method 
  
// Import Queue 
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating queue
        val s1 = Queue("g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s") 
          
        // Print the queue
        println(s1) 
          
        // Applying distinct() method  
        val result = s1.distinct
          
        // Display output 
        print("Queue with distinct elements: " + result) 
           
    } 
} 

输出。

Queue(g, e, e, k, s, f, o, r, g, e, e, k, s)
Queue with distinct elements: Queue(g, e, k, s, f, o, r)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程