Skip to main content
ubuntuask.com

Back to all posts

How to Convert A Nullable Mutablemap to Not Nullable In Kotlin?

Published on
5 min read
How to Convert A Nullable Mutablemap to Not Nullable In Kotlin? image

Best Kotlin Programming Guides to Buy in October 2025

1 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
2 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
3 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
7 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
8 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.62
Kotlin: An Illustrated Guide
9 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
10 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$30.53 $44.99
Save 32%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
+
ONE MORE?

To convert a nullable MutableMap to a not nullable one in Kotlin, you can follow the steps below:

  1. Firstly, check if the nullable MutableMap is not null. If it is null, you can assign an empty map to the non-nullable MutableMap. This step ensures that the non-nullable map is initialized even if the nullable map is null.

val nullableMap: MutableMap<String, String>? = // nullable map

val nonNullableMap: MutableMap<String, String> = nullableMap ?: mutableMapOf()

  1. If the nullable map is not null, and you want to filter out any null values from the map, you can use the filterValues function. This function returns a new map containing only the key-value pairs from the original map where the value is not null.

val nullableMap: MutableMap<String, String>? = // nullable map

val nonNullableMap: MutableMap<String, String> = nullableMap?.filterValues { it != null }?.toMutableMap() ?: mutableMapOf()

  1. After performing the above steps, you will have a non-nullable MutableMap that you can safely use without worrying about nullability.

Note: Depending on your specific use case, you may need to modify the null value check in step 2 to suit your requirements.

How to convert a nullable mutableMap to a non-nullable LinkedHashMap in Kotlin?

In Kotlin, you can convert a nullable MutableMap to a non-nullable LinkedHashMap using the safe call operator ?. and the toMutableMap() and toMap() methods.

Here's an example:

val nullableMap: MutableMap<String, Int>? = mutableMapOf("one" to 1, "two" to 2)

val nonNullableMap: LinkedHashMap<String, Int> = nullableMap?.toMap()?.toMutableMap() as LinkedHashMap<String, Int>

println(nonNullableMap)

In the code above:

  1. Create a nullable MutableMap called nullableMap initialized with a mutable map.
  2. Use the safe call operator ?. to safely call the toMap() method on nullableMap. The toMap() method returns a regular Map.
  3. Use the toMutableMap() method on the regular Map to convert it back to a mutable map.
  4. Cast the resulting mutable map to a LinkedHashMap using the as keyword to make it non-nullable.
  5. Finally, print nonNullableMap to verify the conversion.

Note that if the original nullableMap is null, the conversion will return null as well. Therefore, you might want to add nullability checks before performing the conversion or use the Elvis operator if you want to provide a default value when nullableMap is null.

How to retrieve values from a nullable mutableMap in Kotlin?

To retrieve values from a nullable mutableMap in Kotlin, you can use the safe operator ?. or the Elvis operator ?:.

Here are two ways to retrieve values from a nullable mutableMap:

  1. Using the safe operator ?.: val nullableMap: MutableMap? = mutableMapOf("a" to 1, "b" to 2) val valueA = nullableMap?.get("a") // Retrieve value for key "a" safely val valueC = nullableMap?.get("c") // Retrieve value for key "c" safely (returns null) println(valueA) // Output: 1 println(valueC) // Output: null
  2. Using the Elvis operator ?:: val nullableMap: MutableMap? = mutableMapOf("a" to 1, "b" to 2) val valueA = nullableMap?.get("a") ?: 0 // Retrieve value for key "a" or default to 0 val valueC = nullableMap?.get("c") ?: -1 // Retrieve value for key "c" or default to -1 println(valueA) // Output: 1 println(valueC) // Output: -1

In both cases, the nullableMap?.get("key") expression ensures that the value retrieval is only performed if the map is not null. If the map is null, the expression returns null. The Elvis operator ?: allows you to provide a default value in case the key is not found or the map is null.

How to convert a nullable mutableMap to a non-nullable TreeMap in Kotlin?

To convert a nullable mutableMap to a non-nullable TreeMap in Kotlin, you can follow these steps:

  1. First, ensure that the nullable mutableMap is not null. If it is null, you can handle the null case according to your requirements, such as returning a default TreeMap or throwing an exception.
  2. Create a new non-nullable TreeMap instance by passing the nullable mutableMap to the TreeMap constructor.
  3. The TreeMap constructor will automatically convert the mutableMap into a TreeMap with the same key-value pairs. If the original mutableMap is modified after the conversion, the TreeMap will not reflect those changes.
  4. Now you have a non-nullable TreeMap object that you can use in your code.

Here's an example implementation:

fun convertNullableMapToTreeMap(nullableMap: MutableMap<String, String>?): TreeMap<String, String> { // Handling the nullable case if (nullableMap == null) { // return a default TreeMap or throw an exception return TreeMap() }

// Converting the nullable mutableMap to a non-nullable TreeMap
return TreeMap(nullableMap)

}

fun main() { val nullableMap: MutableMap<String, String>? = mutableMapOf("key1" to "value1", "key2" to "value2")

val nonNullableTreeMap = convertNullableMapToTreeMap(nullableMap)

// Testing the non-nullable TreeMap
nonNullableTreeMap\["key3"\] = "value3"
println(nonNullableTreeMap) // output: {key1=value1, key2=value2, key3=value3}

}

In this example, convertNullableMapToTreeMap function takes a nullableMap as input and returns a non-nullable TreeMap by calling the TreeMap constructor with the nullableMap parameter. If the nullableMap is null, it returns a default TreeMap. In the main function, we test the non-nullable TreeMap by adding a new key-value pair and printing the result.