Home > database >  Changing the value in Map
Changing the value in Map

Time:11-27

I'm studying kotlin. Now I'm solving a problem: if there is a key in the map, then substract one, and if there is not, then put 10. I wrote the code, but maybe it can be simplified without using an intermediate variable?

    val map = mutableMapOf<String, String>()

    val value = map["key"]
    map["key"] = if (value != null) (value.toInt()-1).toString() else "10"

CodePudding user response:

For Kotlin/JVM, you can use compute, and use this really nice nullable chaining in the lambda:

map.compute("key") { _, value ->
    // value would be null if the key doesn't exist in the map
    // so we chain the null all the way until we get the final string
    value?.toInt()?.minus(1)?.toString() ?: "10"
}

Though I would also suggest that you use a MutableMap<String, Int> instead, to avoid all the intermediate toInt and toString mess. Then you can just do:

map.compute("key") { _, value ->
    value?.minus(1) ?: 10
}

If you want a strings map later on, just mapValues.

CodePudding user response:

for a better performance i recommend using fastutil

add this to your pom file

    <dependency>
        <groupId>it.unimi.dsi</groupId>
        <artifactId>fastutil</artifactId>
        <version>8.1.1</version>
    </dependency>

then in case, there isn't any key your put 10 else you subtract 1

import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap

    fun main() {
        val key = "key"
        val map = Object2IntOpenHashMap<String>()
        if (map.putIfAbsent(key, 10) > 0) map.addTo(key, -1)
    }

CodePudding user response:

Your current code seems good to me. If you really want to avoid an intermediate variable, just replace value with map["key"] everywhere.

Other ways to do the same thing can be:

map["key"] = map["key"]?.toInt()?.minus(1)?.toString() ?: "10"
OR
map["key"] = map["key"].let { if (it != null) (it.toInt() - 1).toString() else "10" }

But I feel that the original code looks more readable than these.
Btw if the map contains only numbers, you can simply use a mutableMapOf<String, Int> and avoid the toInt() and toString() calls. And if it doesn't always contain numbers, you might want to use toIntOrNull() instead of toInt()

CodePudding user response:

You can use Map.putIfAbsent method.

Method description: if the specified key is not already associated with a value or is mapped to null associates it with the given value and returns null, else returns the current value:

map.putIfAbsent("key","10")?.let{map["key"]=(it.toInt()-1).toString()}

CodePudding user response:

I would do it like this

map["key"] = map["key"]?.let{(it.toInt() - 1).toString()} ?: "10"
  • Related