Scala Iterator sameElements() method with example

The sameElements() method belongs to the concrete value members of the class iterator of Scala. It checks whether the two stated iterators produce the identical elements in alike order or not. This method won’t terminate for infinite iterators.

  • Method Definition:

    def sameElements(that: Iterator[_]): Boolean
    

    Where, that is the another iterator stated.

  • Return Type:
    It returns true if both the iterators produce identical elements in the same order else it returns false.

Example-1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of sameElements()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(3, 4, 5, 6, 7)
          
        // Declaring an another iterator
        val iter1 = Iterator(3, 4, 5, 6, 7)
          
        // Applying sameElements method 
        val result = iter.sameElements(iter1)
          
        // Displays output
        println(result)
  
    }
}

chevron_right


Output:

true

Here, both the stated iterators are identical so, sameElements method returns true.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of sameElements()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(3, 4, 5, 6, 7)
          
        // Declaring an another iterator
        val iter1 = Iterator(3, 4, 5, 9, 7)
          
        // Applying sameElements method 
        val result = iter.sameElements(iter1)
          
        // Displays output
        println(result)
  
    }

chevron_right


Output:

false

Here, both the stated iterators are not identical so, sameElements method returns false.



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.