Skip to content
Related Articles

Related Articles

Scala List iterator() method with example
  • Last Updated : 29 Jul, 2019

The iterator method is utilized to give an iterator.

Method Definition: def iterator: Iterator[A]

Return Type: It returns a non-empty iterator for non-empty list and returns an empty iterator for empty list.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of iterator
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(1, 2, 3, 4, 5)
          
        // Applying iterator method
        val result = m1.iterator
          
        // Displays output
        println(result)
          
    }
}

chevron_right


Output:

non-empty iterator

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of iterator
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an empty list
        val m1 = List() 
          
        // Applying iterator method
        val result = m1.iterator
          
        // Displays output
        println(result)
      
    }
}

chevron_right


Output:

empty iterator



My Personal Notes arrow_drop_up
Recommended Articles
Page :