Sling Academy
Home/Kotlin/Combining Safe Calls with Collections in Kotlin

Combining Safe Calls with Collections in Kotlin

Last updated: December 05, 2024

Kotlin, a modern programming language developed by JetBrains, is widely popular for its amazing features that improve safety and readability. Among its noteworthy features are null safety and enhanced support for collections. In this article, we will explore how to leverage safe calls on collections in Kotlin, making your code more secure and concise.

Understanding Null Safety

One of the pain points in many programming languages is null reference exceptions, often leading to application crashes if not handled properly. Kotlin addresses this problem with null safety built into its type system. It distinguishes between nullable and non-nullable types and helps developers handle possible null values effectively.


var nullableList: List? = null
var nonNullableList: List = listOf(1, 2, 3)

In the above example, nullableList can hold a null value, whereas nonNullableList cannot. By default, all types are non-nullable.

Safe Calls with Safe Navigation Operator

Kotlin introduces the safe call operator ?. which helps safely navigate properties and methods of nullable types. This operator lets us safely call methods or access properties of a nullable variable without explicitly checking for null.


println(nullableList?.size) // Safely gets the size or returns null without throwing an exception

Here, we attempt to get the size of nullableList. If nullableList is null, the safe call operator ensures that no attempt is made to access its size, thus avoiding a NullPointerException and simply yielding null.

Applying Safe Calls on Collections

Kotlin offers powerful collection handling capabilities, enabling efficient data manipulation. However, when dealing with nullable collections, safe calls become invaluable. You can perform various operations while guarding against null references using safe calls.


// Filter a nullable list safely
val filteredList = nullableList?.filter { it > 1 }
println(filteredList) // This will print null if nullableList is null

By using ?., we safely apply the filter operation even if nullableList is null. Additional operations can also be performed safely using this approach.

Handling Nested Nullables in Collections

In real-world scenarios, collections might not only be nullable themselves but also contain nullable elements. In such cases, safe calls need to be carefully applied to navigate both the collection and its elements.


// List of lists containing nullable elements
val disjointedList: List?> = listOf(listOf(1, 2, null), null, listOf(3, null, 5))

// Safely execute operations
val flattenedNonNullList = disjointedList
    .filterNotNull() // Removes null lists
    .flatMap { it.filterNotNull() } // Removes null elements within lists

println(flattenedNonNullList) // Outputs: [1, 2, 3, 5]

In this example, we have a List composed of other lists, where both the parent list and its contained lists can have null values. By using safe calls combined with collection operations, we can effectively eliminate nulls.

Chaining Safe Calls with Other Operators

You can also chain safe calls with other useful Kotlin operators. The ?: Elvis operator is particularly useful for providing a default value when a null might be encountered.


// Provide default when null
val sizeOfList = nullableList?.size ?: 0
println(sizeOfList) // Outputs 0 if nullableList is null

In this example, if nullableList is null, the size defaults to 0, preventing code failures while still enabling core logic to execute as expected.

Conclusion

Kotlin's unique handling of null safety through features like safe calls makes it highly resilient against null pointer exceptions, a common source of runtime errors. By understanding and effectively utilizing safe calls in conjunction with collections, developers can write cleaner, safer, and more efficient Kotlin code. Mastering these techniques will save you time debugging and allow you to produce robust applications that enhance user experiences.

Next Article: Filtering Non-Null Values from Lists with `filterNotNull` in Kotlin

Previous Article: How to Use `is` for Nullable Type Checking in Kotlin

Series: Null Safety in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin