I am trying to pull the maximum value form a map along with its key. For example:
val map = Map(\'a\' -> 100, \'b\' -> 23, ... \'z\' -> 56)
You can use maxBy
with a function from the key-value pair to just the value:
val map = Map('a' -> 100, 'b' -> 23, 'z' -> 56)
map.maxBy(_._2) // (a,100)
This is a short form for
map.maxBy { case (key, value) => value }
A slight modification in case the max value you are looking for is present more than once in the map:
// Find the entries with the max value in the map
val maxValue = map.maxBy(item => item._2)
// Filter the map and retain the entries with the max value
map.filter(item => item._2 == maxValue._2)