Skip to content
Related Articles

Related Articles

Improve Article

How to find largest key in Scala Map

  • Last Updated : 26 Aug, 2019

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 -> "geeks", 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :