Find maximum-value key in a Kotlin Map
This article explores different ways to find the maximum-value entry in a Kotlin Map.
1. Using maxBy() function
The recommended solution is to find the maximum value entry in the map is using the maxWith() function that accepts a Comparator to compare objects based on a field value.
|
1 2 3 4 5 6 7 8 |
fun main() { val map: MutableMap<Char, Int> = mutableMapOf( Pair('A', 1), Pair('B', 2), Pair('C', 3), Pair('D', 4) ) val maxEntry = map.maxWith(Comparator { x, y -> x.value.compareTo(y.value)}) println(maxEntry) } |
Output:
D=4
The above code can be further shortened using the maxBy() function, which returns the first entry yielding the largest value of the provided function or null if there are no entries.
|
1 2 3 4 5 6 7 8 |
fun main() { val map: MutableMap<Char, Int> = mutableMapOf( Pair('A', 1), Pair('B', 2), Pair('C', 3), Pair('D', 4) ) val maxEntry = map.maxBy { it.value } println(maxEntry) } |
Output:
D=4
2. Using Collections.max() function
The Collections.max() function returns the maximum element of the specified collection, according to the order induced by the specified comparator. We can use it with Map.Entry.comparingByValue to find the maximum-value entry in the map, as shown below:
|
1 2 3 4 5 6 7 8 9 10 |
import java.util.Collections fun main() { val map: MutableMap<Char, Int> = mutableMapOf( Pair('A', 1), Pair('B', 2), Pair('C', 3), Pair('D', 4) ) val maxEntry = Collections.max(map.entries, java.util.Map.Entry.comparingByValue()) println(maxEntry) } |
Output:
D=4
3. Using Loop
We can also iterate over the map and keep track of the entry with the maximum value. This can be implemented as follows in Kotlin, using a loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun main() { val map: MutableMap<Char, Int> = mutableMapOf( Pair('A', 1), Pair('B', 2), Pair('C', 3), Pair('D', 4) ) var maxEntry: Map.Entry<Char, Int>? = null for (entry in map.entries) { if (maxEntry == null || entry.value > maxEntry.value) { maxEntry = entry } } println(maxEntry) } |
Output:
D=4
We can also iterate over the Map’s keys to find the key with the maximum value:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun main() { val map: MutableMap<Char, Int> = mutableMapOf( Pair('A', 1), Pair('B', 2), Pair('C', 3), Pair('D', 4) ) var maxKey: Char? = null for (key in map.keys) { if (maxKey == null || map[key]!! > map[maxKey]!!) { maxKey = key } } println(maxKey) } |
Output:
D
If multiple keys have the maximum value, the above code returns the first key with the maximum value. To get all keys having the maximum value, we can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun main() { val map: MutableMap<Char, Int> = mutableMapOf( Pair('A', 1), Pair('B', 2), Pair('C', 3), Pair('D', 4) ) val maxValue = map.values.max() val maxValueKeys: MutableList<Char> = mutableListOf() for ((key, value) in map) { if (value == maxValue) { maxValueKeys.add(key) } } println(maxValueKeys) } |
Output:
[B, D]
That’s all about finding the maximum-value entry in a Kotlin Map.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)