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 iter 1 = Iterator( 3 , 4 , 5 , 6 , 7 )
// Applying sameElements method
val result = iter.sameElements(iter 1 )
// 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 iter 1 = Iterator( 3 , 4 , 5 , 9 , 7 )
// Applying sameElements method
val result = iter.sameElements(iter 1 )
// Displays output
println(result)
}
} |
Output:
false
Here, both the stated iterators are not identical so, sameElements method returns false.