Find max int key in hashmap kotlin
val myDict : HashMap<Int, String> = hashMapOf( 0 to "zero", 1 to "one", 2 to "two" )
I have to find maximum Int key value of myDict HashMap. something like this:
myDict.getMaxKey() // should be 2
Solution 1:
In addition to Tom's answer, if you want to have the Map entry and not only the key, you can do something as follows:
myDict.maxByOrNull { it.key })
Solution 2:
You could do this :
myDict.keys.maxOrNull()
Api reference : keys
, maxOrNull()