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


Scala SortedSet ++()用法及代碼示例

++() 方法用於將一個 SortedSet 的元素添加到另一個 SortedSet。

函數定義: def ++(elems: A): SortedSet[A]

返回類型: It returns a new TreeSet containing elements of both the SortedSets.

示例#1:


// Scala program of ++() method   
import scala.collection.immutable.SortedSet  
   
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a SortedSet  
        val s1 = SortedSet(1, 3, 5)  
          
        val s2 = SortedSet(2, 4, 6) 
          
        // Applying ++() method  
        val result = s1 ++ s2
              
        // Display output 
        print(result)  
          
    }  
}  
輸出:
TreeSet(1, 2, 3, 4, 5, 6)

示例#2:


// Scala program of ++() method   
import scala.collection.immutable.SortedSet  
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a SortedSet  
        val s1 = SortedSet(41, 12, 23, 43, 1, 72)  
          
        val s2 = SortedSet(1, 100, 5, 12, 23) 
          
        // Applying ++() method  
        val result = s1 ++ s2
              
        // Display output 
        print(result)    
          
    }  
}  
輸出:
TreeSet(1, 5, 12, 23, 41, 43, 72, 100)


相關用法


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