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


Scala Set diff()用法及代碼示例


diff()方法用於計算一組和另一組的差。

函數定義: def diff(that: Set[A]): Set[A]

返回類型: It returns a set which is the difference between two sets.



範例1:

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

範例2:

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


相關用法


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