Open In App

Scala TreeSet apply() method with example

In Scala TreeSet class, the apply() method is utilized to check if some element is present in the TreeSet or not.

Method Definition: def apply(elem: A): Boolean



Return Type: It returns true if the given element is present in the TreeSet or else returns false.

Example #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)
          
    

Output:

TreeSet(1, 2, 3, 4, 5)
TreeSet contains '3': true

Example #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)
          
    

Output:
TreeSet(1, 2, 3, 4, 5)
TreeSet contains '0': false

Article Tags :