Skip to content
Related Articles

Related Articles

Improve Article

Scala immutable TreeSet toSeq() method

  • Last Updated : 19 Apr, 2020

In Scala immutable TreeSet class the toSeq() method is utilised to return a seq consisting of all the elements of the TreeSet.

Method Definition: def toSeq: Seq[A]

Return Type: It returns a seq consisting of all the elements of the TreeSet.

Example #1:




// Scala program of toSeq() 
// method 
  
// Import TreeSet
import scala.collection.immutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating TreeSet
        val t1 = TreeSet(1, 2, 3, 4, 5, 6)  
            
        // Print the TreeSet 
        println(t1
          
        // Applying toSeq method  
        val result = t1.toSeq
            
        // Display output 
        print("Elements in the Seq: "
            
        for (ele <- result)  
            print(ele + " "
            
    
Output:
TreeSet(1, 2, 3, 4, 5, 6)
Elements in the Seq: 1 2 3 4 5 6

Example #2:




// Scala program of toSeq() 
// method 
  
// Import TreeSet
import scala.collection.immutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating TreeSet
        val t1 = TreeSet("u", "a", "i", "o", "e")  
            
        // Print the TreeSet 
        println(t1
          
        // Applying toSeq method  
        val result = t1.toSeq
            
        // Display output 
        print("Elements in the Seq: "
            
        for (ele <- result)  
            print(ele + " "
            
    
Output:
TreeSet(a, e, i, o, u)
Elements in the Seq: a e i o u



My Personal Notes arrow_drop_up
Recommended Articles
Page :