当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Scala Queue distinct()用法及代码示例


distinct()方法用于从给定队列中删除重复的元素。

函数定义: def distinct: Queue[A]

返回类型: It returns a new queue that contains only distinct elements.



范例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)


相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 Scala Queue distinct() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。