Scala Iterator toSeq() method with example

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
          
    }
}

chevron_right


Output:

Stream(2, ?)

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
          
    }
}

chevron_right


Output:

Stream()

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.