Thursday, October 16, 2025
HomeLanguagesHow to find largest key in Scala Map

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:




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


Output:

(4, for)

Example #2: Iterate over only keys using keysIterator




// 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) 
      
    


Output:

Charlie
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS