Open In App

Scala Iterator sameElements() method with example

Last Updated : 28 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

true

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




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


Output:

false

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads