📜  带有示例的 Scala TreeSet apply() 方法

📅  最后修改于: 2022-05-13 01:55:24.713000             🧑  作者: Mango

带有示例的 Scala TreeSet apply() 方法

在 Scala TreeSet class中, apply()方法用于检查 TreeSet 中是否存在某些元素。

示例 #1:

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

示例 #2:

// Scala program of apply() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating TreeSet
        val t1 = TreeSet(2, 1, 3, 4, 5) 
          
        // Print the TreeSet
        println(t1) 
          
        // Applying apply() method  
        val result = t1.apply(0)
          
        // Displays output 
        println("TreeSet contains '0': " + result)
          
    } 
} 
输出:
TreeSet(1, 2, 3, 4, 5)
TreeSet contains '0': false