Open In App

Scala TreeSet toSeq() method with example

Improve
Improve
Like Article
Like
Save
Share
Report

The toSeq() method is utilized 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.mutable._
  
// 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.mutable._
  
// 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


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