Scala Iterator zip() method with example

The zip() method belongs to the concrete value member of the class Abstract Iterator. It is defined in the class Iterator.

Method Definition: val result = iter.zip(iter1)

Return Type: It returns a new Scala iterator holding pairs of corresponding elements in the iterator and the size of the number of elements returned is minimum of the sizes of both the iterators.



Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of zip()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating the first Iterator 
        val iter = Iterator(1, 2, 3, 4, 5, 6)
          
        // Creating the second iterator
        val iter1 = Iterator(7, 8, 9, 10)
          
        // Applying zip method 
        val result = iter.zip(iter1)
          
        // 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 zip()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating the first Iterator 
        val iter = Iterator()
          
        // Creating the second iterator
        val iter1 = Iterator(0, 0, 0, 0)
          
        // Applying zip method 
        val result = iter.zip(iter1)
          
        // Displays output
        println(result)
      
    }
}

chevron_right


Output:

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.