Skip to content
Related Articles

Related Articles

Scala Iterator duplicate() method with example
  • Last Updated : 28 May, 2019
GeeksforGeeks - Summer Carnival Banner

The duplicate() method belongs to the concrete value member of the class iterator. It generates a duplicate of the iterator which will iterate over the alike order of values. The duplicate iterators are said to be equal if they are put at the identical element.

  • Method Definition:
     def duplicate: (Iterator[A], Iterator[A])
    
  • Return Type:
    It returns a pair of iterators.

Example :




// Scala program of duplicate()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(3, 4, 5, 7, 8)
          
        // Applying duplicate method
        val iter1 = iter.duplicate
          
        // Displays output
        println(iter1)
  
    }
Output:
(non-empty iterator, non-empty iterator)

Here, the stated iterator is non-empty so, two non-empty iterators are created.
Example :






// Scala program of duplicate()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an empty-iterator
        val iter = Iterator()
          
        // Applying duplicate method
        val iter1 = iter.duplicate
          
        // Displays output
        println(iter1)
  
    }
Output:
(empty iterator, empty iterator)

Here, the stated iterator is empty so, two empty iterators are created.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :