In order to get all the keys from a Scala map, we need to use either keySet method (to get all the keys as a set) or we can use keys method and if you want to get the keys as an iterator, you need to use keysIterator method. Now, lets check some examples.
Example #1:
// Scala program of keySet() // 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 keySet method val result = m1.keySet // Displays output println(result) } } |
Set(3, 4, 2)
Here, keySet method is utilized.
Example #2:
// Scala program of keys() // 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 keys method val result = m1.keys // Displays output println(result) } } |
Set(3, 4, 2)
Here, keys method is utilized.
Example #3:
// Scala program of keysIterator() // 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 keysIterator method val result = m1.keysIterator // Displays output println(result) } } |
non-empty iterator
Here, keysIterator method is utilized.
Recommended Posts:
- Scala Map keys() method with example
- How to Reverse keys and values in Scala Map
- How to get all the values from a Scala map
- Scala Map get() method with example
- Scala SortedMap keys() method with example
- Scala Mutable SortedMap keys() method with example
- Scala Map size() method with example
- Using anonymous functions with the map method in Scala
- Scala Map toSet() method with example
- Scala Map
- Scala | map() method
- Scala | Methods to Call on a Map | Set-1
- Scala map contains() method with example
- Scala map isDefinedAt() method with example
- Scala Iterator map() method with example
- Remove key value from Map in Scala
- Scala Map clone() method with example
- Scala Map clear() method with example
- Scala Map apply() method with example
- Scala Map addString() method with a separator with example
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.

