Skip to content
Related Articles

Related Articles

Scala immutable TreeSet size() method

Improve Article
Save Article
  • Last Updated : 19 Apr, 2020
Improve Article
Save Article

In Scala immutable TreeSet class the size() method is utilised to return the number of elements present in the TrreSet.

Method Definition: def size: Int

Return Type: It returns the number of elements present in the TreeSet.

Example #1:




// Scala program of size() 
// method 
  
// Import TreeSet
import scala.collection.immutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating TreeSet
        val t1 = TreeSet(3, 1, 5, 2, 4)  
            
        // Print the TreeSet 
        println(t1
          
        // Applying size method  
        val result = t1.size
            
        // Displays output  
        print("Size of the TreeSet: " + result) 
          
    

Output:

TreeSet(1, 2, 3, 4, 5)
Size of the TreeSet: 5

Example #2:




// Scala program of size() 
// method 
  
// Import TreeSet
import scala.collection.immutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating TreeSet
        val t1 = TreeSet(3, 1, 5, 2, 4, 10, 6, 9, 7, 8)  
            
        // Print the TreeSet 
        println(t1
          
        // Applying size method  
        val result = t1.size
            
        // Displays output  
        print("Size of the TreeSet: " + result) 
          
    

Output:

TreeSet(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Size of the TreeSet: 10

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!