How to find largest key in Scala Map

In Scala, Map is same as dictionary which holds key:value pairs. In this article, we will learn how to find the largest key in given Map in Scala. The max() method is utilized to find the largest element of the map.
Syntax:

m1.max 
Here, m1 is name of a map.

Let’s try to understand it better with help of few examples.
Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to get highest keys
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a map 
        val m1 = Map(3 -> "geeks", 4 -> "for", 2 -> "cs"
          
  
        // Displays max key 
        println(m1.max) 
      
    

chevron_right


Output:



(4, for)

Example #2: Iterate over only keys using keysIterator

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to get highest keys
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating a map 
        val mapIm = Map("Ajay" -> 30
                        "Bhavesh" -> 20
                        "Charlie" -> 50
          
        // Displays output 
        println(mapIm.keysIterator.max) 
      
    

chevron_right


Output:

Charlie


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.