为什么 mutableMapOf returns 可以为空?

Why mutableMapOf returns nullable?

我写了一个简单的代码块来将 fragmentsids 存储在 Map 中。地图的 valuekey 被初始化为不可空,但是当我想将它们作为 keyvalue 时,它会给出 nullable 类型的数据。所以,我不明白为什么? returns non-nullable 数据在 Kotlin 中是否有任何其他类型的地图?

键[位置]可能没有值,这将使其成为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.

@nhaarman 是对的,但是您可以使用 getValue(key) return 不可为空但如果找不到则抛出 NoSuchElementException

/**
 * Returns the value for the given [key] or throws an exception if there is no such key in the map.
 *
 * If the map was created by [withDefault], resorts to its `defaultValue` provider function
 * instead of throwing an exception.
 *
 * @throws NoSuchElementException when the map doesn't contain a value for the specified key and
 * no implicit default value was provided for that map.
 */
@SinceKotlin("1.1")
public fun <K, V> Map<K, V>.getValue(key: K): V = getOrImplicitDefault(key)