Scala排序集apply()方法及示例

Scala排序集apply()方法及示例

apply() 方法是用来测试SortedSet中是否存在某些元素的。

方法定义:def apply(elem: A)

返回类型。如果某些元素存在于SortedSet中,则返回true,否则返回false。

例子 #1:

// Scala program of apply()
// method
import scala.collection.immutable.SortedSet 
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating SortedSets 
        val s1 = SortedSet(1, 2, 3, 4, 5) 
          
        // Applying apply method 
        val result = s1.apply(3) 
          
        // Displays output 
        println(result) 
      
    } 
} 

输出。

true

例子#2。

// Scala program of apply()
// method
import scala.collection.immutable.SortedSet 
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating SortedSets 
        val s1 = SortedSet(1, 2, 3, 4, 5) 
          
        // Applying apply method 
        val result = s1.apply(10) 
          
        // Displays output 
        println(result) 
      
    } 
} 

输出。

false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程