Skip to content
Advertisement

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

Advertisement

Answer

You could do this :

myDict.keys.maxOrNull()

Api reference : keys, maxOrNull()

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement