The filterNotNull
function in Kotlin is used to filter out all null elements from a collection, returning a new list containing only the non-null elements. This function belongs to the Kotlin standard library and provides a convenient way to ensure that a collection does not contain any null values.
Table of Contents
- Introduction
filterNotNull
Function Syntax- Understanding
filterNotNull
- Examples
- Basic Usage
- Filtering a List of Nullable Integers
- Filtering a List of Nullable Strings
- Real-World Use Case
- Conclusion
Introduction
The filterNotNull
function allows you to create a new list that excludes all null elements from a collection. This is useful for scenarios where you need to work with collections that should not contain any null values, ensuring that subsequent operations on the collection can be performed without null checks.
filterNotNull Function Syntax
The syntax for the filterNotNull
function is as follows:
fun <T : Any> Iterable<T?>.filterNotNull(): List<T>
Parameters:
- This function does not take any parameters.
Returns:
- A new list containing only the non-null elements from the original collection.
Understanding filterNotNull
The filterNotNull
function iterates over each element in the collection and includes only the non-null elements in the resulting list. This ensures that the resulting list contains no null values, making it safer and easier to work with.
Examples
Basic Usage
To demonstrate the basic usage of filterNotNull
, we will filter a list of nullable integers to exclude all null values.
Example
fun main() {
val numbers: List<Int?> = listOf(1, 2, null, 4, null, 6)
val nonNullNumbers: List<Int> = numbers.filterNotNull()
println("Non-null numbers: $nonNullNumbers")
}
Output:
Non-null numbers: [1, 2, 4, 6]
Filtering a List of Nullable Integers
This example shows how to use filterNotNull
to create a new list containing only non-null integers.
Example
fun main() {
val scores: List<Int?> = listOf(10, 20, null, 40, null, 60)
val validScores: List<Int> = scores.filterNotNull()
println("Valid scores: $validScores")
}
Output:
Valid scores: [10, 20, 40, 60]
Filtering a List of Nullable Strings
This example demonstrates how to use filterNotNull
to create a new list containing only non-null strings.
Example
fun main() {
val names: List<String?> = listOf("Alice", null, "Bob", null, "Charlie")
val nonNullNames: List<String> = names.filterNotNull()
println("Non-null names: $nonNullNames")
}
Output:
Non-null names: [Alice, Bob, Charlie]
Real-World Use Case
Cleaning Up User Input
In real-world applications, the filterNotNull
function can be used to clean up user input, ensuring that a list of user-provided values does not contain any nulls before further processing.
Example
fun main() {
val userInputs: List<String?> = listOf("input1", null, "input2", "", null, "input3")
val validInputs: List<String> = userInputs.filterNotNull().filter { it.isNotEmpty() }
println("Valid inputs: $validInputs")
}
Output:
Valid inputs: [input1, input2, input3]
Conclusion
The filterNotNull
function in Kotlin is a powerful and convenient way to create a new list containing only the non-null elements from a collection. It allows you to easily filter out null values, ensuring that the resulting list is safe to work with and free of null-related issues.
By understanding and using the filterNotNull
function, you can effectively manage and process collections in your Kotlin applications.
Comments
Post a Comment
Leave Comment