Scala Iterator toString() method with example

The toString() method belongs to the concrete value member of the class Abstract Iterator. It is utilized to convert the stated iterator to a string.

Method Definition: def toString(): String

Return Type: It returns a string from the stated iterator.



Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

non-empty iterator

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

empty iterator

In above example, iterator is empty so produced result is also empty iterator.



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.