SCALA Iterators & Arrays With Examples


Scala iterator is not a collection, but rather a way to access the elements of a collection one by one.

Here two basic operations on iterators

2. hasNext

A Call to will return the element of the iterator and advance the state of the iterator.

A Call to it.hasNext() will return more elements of the iterator


scala > val itObject = Iterator (“Hadoop”, “Scala”, “Spark”);

itObject: Iterator[String] = non – empty iterator

scala > while (itObject.hasNext) {

println(” ” +}


Scala  Spark

scala > println (“Size of Iterator : ” +itObject. size)

Size of Iterator : 3


Scala array is a normal array. To create an array of integers below like this:

scala > var arrayEle = Array (1,2,3,4,5,6,7,8) ;

arrayEle: Array[Int] = Array (1,2,3,4,5,6,7,8)

scala > for (x < – arrayEle) {

println(“Array elements”+ x)}

scala > println (arrayEle. length)

O/p: 8

Leave a Reply

Your email address will not be published. Required fields are marked *