An iterator is a way to access elements of a collection one-by-one. It resembles to a collection in terms of syntax but works differently in terms of functionality. An iterator defined for any collection does not load the entire collection into the memory but loads elements one after the other. Therefore, iterators are useful when the data is too large for the memory. To access elements we can make use of hasNext() to check if there are elements available and next() to print the next element.
Syntax:
val v = Iterator(5, 1, 2, 3, 6, 4)
//checking for availability of next element
while(v.hasNext)
//printing the element
println(v.next)
Defining an iterator for a collection
We can define an iterator for any collection(Arrays, Lists, etc) and can step through the elements of that particular collection.
Example:
object GFG
{
def main(args : Array[String])
{
val v = Array( 5 , 1 , 2 , 3 , 6 , 4 )
val i = v.iterator
while (i.hasNext)
print(i.next + " " )
}
}
|
Output:
5 1 2 3 6 4
Ways to access elements
- Using while loop: Simplest way to access elements is to use while loop along with hasNext and next methods.
object GFG
{
def main(args : Array[String])
{
val i = Iterator( 5 , 1 , 2 , 3 , 6 , 4 )
while (i.hasNext)
println(i.next)
}
}
|
Output:
5
1
2
3
6
4
- Using foreach action: We can make use of foreach to print elements by passing println function as parameter. Note that foreach is a higher order function that takes another function as parameter. In other words, println function is applied on every element.
object GFG
{
def main(args : Array[String])
{
val i = Iterator( 5 , 1 , 2 , 3 , 6 , 4 )
i foreach println
}
}
|
Output:
5
1
2
3
6
4
- Using for loop: Another straightforward way is to use for loop. It works in very similar way as accessing elements of any collection using for loop.
object GFG
{
def main(args : Array[String])
{
val i = Iterator( 5 , 1 , 2 , 3 , 6 , 4 )
for (k < - i) println(k)
}
}
|
Output:
5
1
2
3
6
4
Finding elements with minimum and maximum values
- Using built-in functions min and max Iterators can be traversed only once. Therefore, we should redefine the iterator after finding the maximum value.
object GFG
{
def main(args : Array[String])
{
val i 1 = Iterator( 5 , 1 , 2 , 3 , 6 , 4 )
println( "Maximum: " + i 1 .max)
val i 2 = Iterator( 5 , 1 , 2 , 3 , 6 , 4 )
println( "Minimum: " + i 2 .min)
}
}
|
Output:
Maximum: 6
Minimum: 1
User-defined functions to return minimum and maximum value. we can define our own pure functions as per our convenience to print minimum and maximum valued element.
Following code prints the minimum valued element:
object GFG
{
def small(ite : Iterator[Int]) : Int =
{
var mn = ite.next
for (i < - ite)
if (i < mn) mn = i
mn
}
def main(args : Array[String])
{
val i = Iterator( 5 , 1 , 2 , 3 , 6 , 4 )
println( "Minimum: " + small(i))
}
}
|
Output:
Minimum: 1
Following code prints the maximum valued element:
object GFG
{
def large(ite : Iterator[Int]) : Int =
{
var mx = ite.next
for (i < - ite)
if (i > mx) mx = i
mx
}
def main(args : Array[String])
{
val i = Iterator( 5 , 1 , 2 , 3 , 6 , 4 )
println( "Maximum: " + large(i))
}
}
|
Output:
Maximum: 6
NOTE: Both functions large() and small() are written as two separate codes to reduce burden on online compiler.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Sep, 2019
Like Article
Save Article