How to get all the values from a Scala map

In order to get all the values from a Scala map, we need to use values method (to get all the values as an Iterable) and if we want to get the values as an iterator, we need to use valuesIterator method. Now, lets check some examples.
Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

MapLike(geeks, for, cs)

Here, values method is utilized.
Example #2:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

non-empty iterator

Here, valuesIterator method is utilized.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.