How to get all the keys from a Scala map Last Updated : 29 Jul, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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) } } Output: Set(3, 4, 2) Here, keySet method is utilized. Example #2: Scala // 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) } } Output: Set(3, 4, 2) Here, keys method is utilized. Example #3: Scala // 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) } } Output: non-empty iterator Here, keysIterator method is utilized. Comment More infoAdvertise with us Next Article How to get all the keys from a Scala map N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-Map Similar Reads 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: Scala // Scala program of values() // method // Creat 1 min read How to sort a Scala Map by key Map is same as dictionary which holds key:value pairs. In this article, we will learn how to sort a Scala Map by key. We can sort the map by key, from low to high or high to low, using sortBy. Syntax : mapName.toSeq.sortBy(_._1):_* Let's try to understand it with better example. Example #1: Scala // 2 min read How to go To and From Java Collections in Scala? In their respective languages, Java collections and Scala collections are two distinct sets of data structures that might be frequently utilized. Although Java collections are a factor of the Java Standard Library, Scala collections are made expressly to combine with the useful programming features 2 min read Scala | Methods to Call on a Map | Set-1 Prerequisite- Scala Map. In Scala there are foremost Methods to call on a Map. A Scala Method is a section of a class which has a designation, a signature, optionally a few annotations, and whatsoever byte-code. A function which is interpreted as a member of some Object is termed as a Method and the 12 min read How to Convert Scala Map into JSON String? In this article, we will learn to convert a Scala Map into JSON String. We can convert Scala Map to JSON String using various libraries and custom conversion functions. Table of Content What is Scala Map?What is JSON String?Converting Scala Map into JSON StringWhat is Scala Map?In Scala, a Map is a 4 min read Like