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:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( 3 - > "geeks" , 4 - > "for" , 2 - > "cs" )
println(m 1 .max)
}
}
|
Output:
(4, for)
Example #2: Iterate over only keys using keysIterator
object GfG
{
def main(args : Array[String])
{
val mapIm = Map( "Ajay" - > 30 ,
"Bhavesh" - > 20 ,
"Charlie" - > 50 )
println(mapIm.keysIterator.max)
}
}
|
Output:
Charlie
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...