Open In App
Related Articles

Scala TreeSet contains() method with example

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
In Scala TreeSet class, the contains() method is utilized to check if an element is present in the TreeSet of not.
Method Definition: def contains(elem: A): Boolean Return Type: It returns true if the element is present in the TreeSet or else it returns false.
Example #1:
// Scala program of contains() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating TreeSet
        val t1 = TreeSet(41, 12, 23, 43, 1, 72
          
        // Print the TreeSet
        println(t1
          
        // Applying contains() method  
        val result = t1.contains(10)
          
        // Display output 
        print("TreeSet contains '10': " + result) 
           
    

                    
Output:
TreeSet(1, 12, 23, 41, 43, 72)
TreeSet contains '10': false
Example #2:
// Scala program of contains() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating TreeSet
        val t1 = TreeSet("g", "e", "e", "k", "s"
          
        // Print the TreeSet
        println(t1
          
        // Applying contains() method  
        val result = t1.contains("k")
          
        // Display output 
        print("TreeSet contains 'k': " + result) 
           
    

                    
Output:
TreeSet(e, g, k, s)
TreeSet contains 'k': true


Last Updated : 03 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads