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:




// 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.



Contact Us