🎓 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 setOfNotNull function in Kotlin is used to create a set of non-null elements. This function belongs to the Kotlin standard library and provides a straightforward way to filter out null values from a set initialization.
Table of Contents
- Introduction
setOfNotNullFunction Syntax- Understanding
setOfNotNull - Examples
- Basic Usage
- Combining Nullable and Non-Nullable Elements
- Creating a Set from a Nullable Collection
- Real-World Use Case
- Conclusion
Introduction
The setOfNotNull function is a convenient way to create a set while automatically excluding any null values. This is useful for initializing sets where some elements may be null, ensuring that the resulting set contains only non-null elements.
setOfNotNull Function Syntax
The syntax for the setOfNotNull function is as follows:
fun <T : Any> setOfNotNull(vararg elements: T?): Set<T>
Parameters:
elements: A variable number of elements, which can include null values.
Returns:
- A set containing only the non-null elements from the provided arguments.
Understanding setOfNotNull
The setOfNotNull function filters out any null values from the provided elements and creates a set containing only the non-null elements. This ensures that the resulting set does not contain any nulls, making it safer to use in situations where null values are not desired.
Examples
Basic Usage
To demonstrate the basic usage of setOfNotNull, we will create a set with some null values.
Example
fun main() {
val set = setOfNotNull(1, null, 2, null, 3)
println("Set of non-null values: $set")
}
Output:
Set of non-null values: [1, 2, 3]
Combining Nullable and Non-Nullable Elements
This example shows how to use setOfNotNull with a mix of nullable and non-nullable elements.
Example
fun main() {
val name1: String? = "Alice"
val name2: String? = null
val name3: String? = "Bob"
val names = setOfNotNull(name1, name2, name3)
println("Set of non-null names: $names")
}
Output:
Set of non-null names: [Alice, Bob]
Creating a Set from a Nullable Collection
This example demonstrates how to create a set from a collection that may contain null values.
Example
fun main() {
val numbers: List<Int?> = listOf(1, null, 2, null, 3, null)
val nonNullNumbers = setOfNotNull(*numbers.toTypedArray())
println("Set of non-null numbers: $nonNullNumbers")
}
Output:
Set of non-null numbers: [1, 2, 3]
Real-World Use Case
Filtering User Input
In real-world applications, the setOfNotNull 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<String?> = listOf("Alice", null, "Bob", "", null, "Charlie")
val validInputs = setOfNotNull(*userInputs.toTypedArray()).filter { it.isNotEmpty() }
println("Set of valid inputs: $validInputs")
}
Output:
Set of valid inputs: [Alice, Bob, Charlie]
Conclusion
The setOfNotNull function in Kotlin is a powerful and convenient way to create sets that exclude null values. It ensures that the resulting set contains only non-null elements, making it useful for various applications, including data processing, filtering user input, and initializing sets with mixed nullable and non-nullable elements.
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