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


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


diff()方法用於查找兩個TreeSet之間的差異。它從另一個TreeSet中刪除一個TreeSet中存在的元素。

函數定義: def diff[B >: A](that: collection.Seq[B]): TreeSet[A]

返回類型: It returns a new TreeSet which consists of elements after the difference between the two given TreeSet.



範例1:

// Scala program of diff()  
// method  
  
// Import TreeSet 
import scala.collection.mutable._
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
      
        // Creating TreeSet 
        val t1 = TreeSet(1, 2, 3, 4, 5)   
            
        val t2 = TreeSet(3, 4, 5)   
            
        // Print the TreeSet  
        println("TreeSet_1: " + t1)  
            
        println("TreeSet_2: " + t2)  
            
        // Applying diff method   
        val result = t1.diff(t2)   
            
        // Displays output   
        print("(TreeSet_1 - TreeSet_2): " + result)  
          
    }  
} 
輸出:
TreeSet_1: TreeSet(1, 2, 3, 4, 5)
TreeSet_2: TreeSet(3, 4, 5)
(TreeSet_1 - TreeSet_2): TreeSet(1, 2)

範例2:

// Scala program of diff()  
// method  
  
// Import TreeSet 
import scala.collection.mutable._
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
      
        // Creating TreeSet 
        val t1 = TreeSet(1, 2, 3, 4, 5)   
            
        val t2 = TreeSet(3, 4, 5, 6, 7, 8)   
            
        // Print the TreeSet  
        println("TreeSet_1: " + t1)  
            
        println("TreeSet_2: " + t2)  
            
        // Applying diff method   
        val result = t2.diff(t1)   
            
        // Displays output   
        print("(TreeSet_2 - TreeSet_1): " + result)  
          
    }  
} 
輸出:
TreeSet_1: TreeSet(1, 2, 3, 4, 5)
TreeSet_2: TreeSet(3, 4, 5, 6, 7, 8)
(TreeSet_2 - TreeSet_1): TreeSet(6, 7, 8)


相關用法


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