當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Scala mutable SortedSet diff()用法及代碼示例

在Scala可變集合中,使用diff()方法來計算SortedSet和另一個SortedSet的差。

Method Definition: def diff(that:SortedSet[A]):SortedSet[A]

Return Type: It returns a SortedSet which is the difference between two SortedSets.

範例1:

// Scala program of diff() 
// method 
import scala.collection.mutable.SortedSet  
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
      
        // Creating SortedSets  
        val s1 = SortedSet(1, 2, 3, 4, 5) 
          
        val s2 = SortedSet(1, 2, 3) 
          
        // Applying diff method  
        val s3 = s1.diff(s2)  
          
        // Displays output  
        for(elem <- s3)   
        println(elem)  
      
    }  
} 
輸出:
4
5

範例2:

// Scala program of diff() 
// method 
import scala.collection.mutable.SortedSet  
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
      
        // Creating SortedSets  
        val s1 = SortedSet(1, 2, 3, 4, 5) 
          
        val s2 = SortedSet(6, 2, 7, 8) 
          
        // Applying diff method  
        val s3 = s1.diff(s2)  
          
        // Displays output  
        for(elem <- s3)   
        println(elem)  
      
    }  
} 
輸出:
1
3
4
5



相關用法


注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Scala mutable SortedSet diff() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。