Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Scala Iterator toSeq() method with example

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The toSeq() method belongs to the concrete value member of the class Abstract Iterator. It is equivalent to the Seq method but this method is more faster.

Method Definition: val result = iter.toSeq

Return Type: It returns the stated collection as a sequence.

Example #1:




// Scala program of toSeq()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a Iterator 
        val iter = Iterator(2, 5, 7, 8, 9)
          
        // Applying toSeq method 
        val result = iter.toSeq
          
        // Displays output
        println(result)
          
    }
}

Output:

Stream(2, ?)

So, here a sequence is obtained.
Example #2:




// Scala program of toSeq()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an empty Iterator 
        val iter = Iterator()
          
        // Applying toSeq method 
        val result = iter.toSeq
          
        // Displays output
        println(result)
          
    }
}

Output:

Stream()

So, here an empty iterator is obtained as the stated iterator is empty.


My Personal Notes arrow_drop_up
Last Updated : 13 Aug, 2019
Like Article
Save Article
Similar Reads