Increment value of a map in Kotlin
This post will discuss several methods to increment the value of a map in Kotlin. If no key/value pair is found for the specified key, initialize the key with value 1.
1. Checking for null
The straightforward solution is to get the value for the specified key. If the key/value pair is present, increment the value by 1; otherwise, add the key with a value of 1.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun <K> increment(map: MutableMap<K, Int?>, key: K) { when (val count = map[key]) { null -> map[key] = 1 else -> map[key] = count + 1 } } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 increment(map, "A") increment(map, "B") println(map) // {A=2, B=1} } |
2. Using putIfAbsent() function
You can use the putIfAbsent() function to check if the key/value pair exists for the specified key or not. If not, you create the key/value pair of the specified key with the value 0. Finally, you increment the value by 1.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun <K> increment(map: MutableMap<K, Int?>, key: K) { map.putIfAbsent(key, 0) map[key] = map[key]!! + 1 } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 increment(map, "A") increment(map, "B") println(map) // {A=2, B=1} } |
3. Using getOrDefault() function
The getOrDefault() function returns the value of the specified key or the default value when no key/value pair exists for the given key. We can use this as:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun <K> increment(map: MutableMap<K, Int?>, key: K) { val count = map.getOrDefault(key, 0) map[key] = count!! + 1 } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 increment(map, "A") increment(map, "B") println(map) // {A=2, B=1} } |
4. Using merge() function
You can also use the merge() function to increment the current value by the 1 with the remapping function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun <K> increment(map: MutableMap<K, Int?>, key: K) { map.merge(key, 1, { a: Int?, b: Int? -> a!! + b!! }) } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 increment(map, "A") increment(map, "B") println(map) // {A=2, B=1} } |
5. Using computeIfPresent() function
The computeIfPresent() returns null if no value is associated with the specified key; otherwise, it computes a new key/value pair given the key and its existing value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun <K> increment(map: MutableMap<K, Int?>, key: K) { if (map.computeIfPresent(key, { _, v -> v + 1 }) == null) { map[key] = 1 } } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 increment(map, "A") increment(map, "B") println(map) // {A=2, B=1} } |
6. Using containsKey() function
Another plausible way is to use the standard native function containsKey() to check if the map contains a key/value pair for a key or not.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun <K> increment(map: MutableMap<K, Int?>, key: K) { val value = if (map.containsKey(key)) map[key] else 0 map[key] = value!! + 1 } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 increment(map, "A") increment(map, "B") println(map) // {A=2, B=1} } |
7. Using AtomicInteger class
You can also use getAndIncrement() or incrementAndGet() function from AtomicInteger class for incrementing the value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.concurrent.atomic.AtomicInteger fun <K> increment(map: MutableMap<K, Int?>, key: K) { val value = if (map.containsKey(key)) map[key] else 0 map[key] = AtomicInteger(value!!).incrementAndGet() } fun main() { val map: MutableMap<String?, Int?> = HashMap() map["A"] = 1 increment(map, "A") increment(map, "B") println(map) // {A=2, B=1} } |
That’s all about incrementing the value of a map in Kotlin.
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 :)