Skip to content
Related Articles

Related Articles

Scala Map iterator method with example
  • Last Updated : 13 Aug, 2019

The iterator method is utilized to give an iterator.

Method Definition: def iterator: Iterator[(A, B)]

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

Example #1:




// Scala program of iterator
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a map
        val m1 = Map("geeks" -> 5, "for" -> 3, "cs" -> 2
          
        // Applying iterator method
        val result = m1.iterator
          
        // Displays output
        println(result)
      
    }
}
Output:
non-empty iterator

Example #2:




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



My Personal Notes arrow_drop_up
Recommended Articles
Page :