Easy Tutorial
❮ Functions Call By Name Scala Operators ❯

Scala Iterator (Iterator)

Scala Collections

A Scala Iterator (iterator) is not a collection; it is a method for accessing a collection.

The two basic operations of an iterator it are next and hasNext.

Calling it.next() returns the next element of the iterator and updates the state of the iterator.

Calling it.hasNext() is used to check if there are any more elements in the collection.

The simplest way to let the iterator it return all elements one by one is by using a while loop:

Example

object Test {
  def main(args: Array[String]) {
    val it = Iterator("Baidu", "Google", "tutorialpro", "Taobao")

    while (it.hasNext){
      println(it.next())
    }
  }
}

Executing the above code, the output will be:

$ scalac Test.scala 
$ scala Test
Baidu
Google
tutorialpro
Taobao

Finding Maximum and Minimum Elements

You can use the it.min and it.max methods to find the maximum and minimum elements from the iterator, as shown in the example below:

Example

object Test {
  def main(args: Array[String]) {
    val ita = Iterator(20,40,2,50,69, 90)
    val itb = Iterator(20,40,2,50,69, 90)

    println("The maximum element is: " + ita.max )
    println("The minimum element is: " + itb.min )
  }
}

Executing the above code, the output will be:

$ scalac Test.scala 
$ scala Test
The maximum element is: 90
The minimum element is: 2

Getting the Length of an Iterator

You can use the it.size or it.length methods to see the number of elements in the iterator. Here is an example:

Example

object Test {
  def main(args: Array[String]) {
    val ita = Iterator(20,40,2,50,69, 90)
    val itb = Iterator(20,40,2,50,69, 90)

    println("The value of ita.size: " + ita.size )
    println("The value of itb.length: " + itb.length )
  }
}

Executing the above code, the output will be:

$ scalac Test.scala 
$ scala Test
The value of ita.size: 6
The value of itb.length: 6

Common Methods of Scala Iterator

The following table lists the commonly used methods of Scala Iterator:

No. Method and Description
1 def hasNext: Boolean Returns true if there is another element to return.
2 def next(): A Returns the next element of the iterator and updates the iterator's state
3 def ++(that: => Iterator[A]): Iterator[A] Combines two iterators
4 def ++B >: A: Iterator[B] Combines two iterators
5 def addString(b: StringBuilder): StringBuilder Adds a string to StringBuilder b
6 def addString(b: StringBuilder, sep: String): StringBuilder Adds a string to StringBuilder b with a specified separator
7 def buffered: BufferedIterator[A] Converts all iterators into BufferedIterator
8 def contains(elem: Any): Boolean Checks if the specified element is in the iterator
9 def copyToArray(xs: Array[A], start: Int, len: Int): Unit Copies selected values from the iterator to an array
10 def count(p: (A) => Boolean): Int Returns the total number of elements in the iterator that meet the condition p.
11 def drop(n: Int): Iterator[A] Returns a new collection of elements, discarding the first n elements
12 def dropWhile(p: (A) => Boolean): Iterator[A] Discards elements from left to right until the condition p is not met
13 def duplicate: (Iterator[A], Iterator[A]) Generates two iterators that can each return all elements of the iterator
14 def exists(p: (A) => Boolean): Boolean Returns a Boolean value indicating whether there is an element in the iterator that meets p
15 def filter(p: (A) => Boolean)
❮ Functions Call By Name Scala Operators ❯