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


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

通過將函數應用於此SortedSet的所有元素,map()方法用於構建新的SortedSet。

函數定義: def map[B](f: (A) => B): immutable.SortedSet[B]

返回類型: It returns a new SortedSet containing all the elements after applying the given function.



範例1:

// Scala program of map()  
// method  
import scala.collection.immutable.SortedSet  
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a SortedSet  
        val s1 = SortedSet(5, 1, 3, 2, 4)  
          
        // Applying map method  
        val result = s1.map(x => x*x) 
          
        // Display output 
        println(result) 
    }  
} 
輸出:
TreeSet(1, 4, 9, 16, 25)

範例2:

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


相關用法


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