How to Reverse keys and values in Scala Map Last Updated : 13 Aug, 2019 Comments Improve Suggest changes Like Article Like Report In Scala, Map is same as dictionary which holds key:value pairs. In this article, we will learn how to reverse the keys and values in given Map in Scala. After reversing the pairs, keys will become values and values will become keys. We can reverse the keys and values of a map with a Scala for-comprehension. Keys should be unique for reverse these values otherwise we can lose some content. Syntax: val reverseMap = for ((k,v) <- map) yield (v, k) Below is the example to reverse keys and values in Scala Map. Example #1: Scala // Scala program to reverse keys and values // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a map val m1 = Map(3 -> "geeks", 4 -> "for", 2 -> "cs") // reversing key:value pairs val reverse = for ((k, v) <- m1) yield (v, k) // Displays output println(reverse) } } Output: Map(geeks -> 3, for -> 4, cs -> 2) Example #2: Scala // Scala program to reverse keys and values // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a map val mapIm = Map("Ajay" -> 30, "Bhavesh" -> 20, "Charlie" -> 50) // Applying keySet method val reverse = for ((k, v) <- mapIm) yield (v, k) // Displays output println(reverse) } } Output: Map(30 -> Ajay, 20 -> Bhavesh, 50 -> Charlie) Comment More infoAdvertise with us Next Article How to Reverse keys and values in Scala Map S Shivam_k Follow Improve Article Tags : Python Scala Scala Scala-Method Scala-Map +1 More Practice Tags : python Similar Reads Remove key value from Map in Scala The deletion of Scala map keys can be obtained by utilizing - operator. This operator is utilized to delete the set of keys of the map. Syntax: def -(elem1: A, elem2: A, elems: A*): Map[A, B] It returns a new map containing all the elements of the given Map except the set of keys which are deleted u 1 min read How to reverse a list in scala In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. We use reverse function to reverse a list in Scala. Below are the examples to reverse a list. Reverse a simple list scala // Scala program to reverse a simp 2 min read How to reverse a String in Scala? In this article, we will learn to reverse a string in Scala. Reverse of a string means changing its order so that the last character becomes the first, the second-to-last becomes the second, and so on, effectively flipping the string's sequence of characters. Examples: Input: S = "GeeksforGeeks"Outp 3 min read How to sort a Scala Map by value In this article, we will learn how to sort a Scala Map by value. We can sort the map by key, from low to high or high to low, using sortBy method. Syntax: MapName.toSeq.sortBy(_._2):_* Let's try to understand it with better example. Example #1: Scala // Scala program to sort given map by value impor 2 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 Like