Open In App

Scala Iterator toString() method with example

Last Updated : 13 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

non-empty iterator

Example #2:




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


Output:

empty iterator

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads