Search code examples
dictionarykotlinnullable

Why mutableMapOf returns nullable?


I wrote a simple code block to store fragments with ids in Map. The value and key of map is initialized as non-nullable but when I want to get them as key and value, it gives nullable type data. So, I don't understand why? Is there any other type of map that returns non-nullable data in Kotlin?

enter image description here


Solution

  • It is possible that there is no value for key [position], which will make it return null:

    abstract operator fun get(key: K): V?
    

    Returns the value corresponding to the given key, or null if such a key is not present in the map.