🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The mapOfNotNull function in Kotlin is used to create a map while filtering out any null values. This function allows you to initialize a map with key-value pairs where null values are excluded, ensuring that the resulting map contains only non-null entries.
Table of Contents
- Introduction
mapOfNotNullFunction Syntax- Understanding
mapOfNotNull - Examples
- Basic Usage
- Combining Nullable and Non-Nullable Values
- Creating a Map from a Collection
- Real-World Use Case
- Conclusion
Introduction
The mapOfNotNull function is a convenient way to create a map while automatically excluding any null values. This is useful for initializing maps where some values may be null, ensuring that the resulting map contains only non-null entries.
mapOfNotNull Function Syntax
The mapOfNotNull function does not exist directly in the Kotlin standard library, but you can create such functionality using extension functions or standard Kotlin functions combined with filtering.
Example Implementation:
Here's how you can implement a mapOfNotNull function using standard Kotlin functions:'
fun <K, V> mapOfNotNull(vararg pairs: Pair<K, V?>): Map<K, V> {
return pairs.filter { it.second != null }.map { it.first to it.second!! }.toMap()
}
Understanding mapOfNotNull
The mapOfNotNull function filters out any pairs where the value is null and creates a map containing only the non-null entries. This ensures that the resulting map does not contain any null values, making it safer to use in situations where null values are not desired.
Examples
Basic Usage
To demonstrate the basic usage of mapOfNotNull, we will create a map with some null values.
Example
fun main() {
val map = mapOfNotNull("a" to 1, "b" to null, "c" to 2)
println("Map of non-null values: $map")
}
Output:
Map of non-null values: {a=1, c=2}
Combining Nullable and Non-Nullable Values
This example shows how to use mapOfNotNull with a mix of nullable and non-nullable values.
Example
fun main() {
val key1: String = "Alice"
val value1: Int? = 25
val key2: String = "Bob"
val value2: Int? = null
val map = mapOfNotNull(key1 to value1, key2 to value2)
println("Map of non-null values: $map")
}
Output:
Map of non-null values: {Alice=25}
Creating a Map from a Collection
This example demonstrates how to create a map from a collection of pairs that may contain null values.
Example
fun main() {
val pairs: List<Pair<String, Int?>> = listOf("a" to 1, "b" to null, "c" to 3)
val map = mapOfNotNull(*pairs.toTypedArray())
println("Map of non-null values: $map")
}
Output:
Map of non-null values: {a=1, c=3}
Real-World Use Case
Filtering User Input
In real-world applications, the mapOfNotNull function can be used to filter user input or data that may contain null values, ensuring that only valid, non-null data is processed.
Example
fun main() {
val userInputs: List<Pair<String, String?>> = listOf("username" to "Alice", "email" to null, "phone" to "1234567890")
val validInputs = mapOfNotNull(*userInputs.toTypedArray())
println("Map of valid inputs: $validInputs")
}
Output:
Map of valid inputs: {username=Alice, phone=1234567890}
Conclusion
While Kotlin does not provide a built-in mapOfNotNull function, you can easily implement such functionality using standard Kotlin functions. This allows you to create maps that exclude null values, ensuring that the resulting map contains only non-null entries.
By understanding and using this approach, you can effectively manage maps in your Kotlin applications while avoiding null-related issues.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment