Lockdown Offer: FLAT 75% + Addl. 25% Off + 30-days Extra Lab on ALL Courses | Use Coupon Code LD25 during Checkout | Offer Expires In

  Enroll Now


44 / 52

Scala - Assessment - Declare an Iterator

Iterator in Scala is a way to access the elements of a collection one by one. The two basic operations on an iterator are next (to access the next item and put the pointer to next to next item) and hasNext (to check whether an iterator has more items or not).

A simple example of declaring and looping over iterator is given below:

val it = Iterator("You", "are", "next")
while (it.hasNext){

An iterator is a collection of items which can be traversed using hasNext() and next() to get the next item. An iterator can be traversed only once after which items are lost.


Declare an Iterator named myIter, having below items.


  • Switch to Jupyter tab.
  • Declare the iterator variable in Jupyter.
  • Press Shift+Enter.
  • Submit your answer.

Note - Having trouble with the assessment engine? Check this discussion