You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/tour/kotlin-tour-intermediate-null-safety.md
+27-11Lines changed: 27 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -191,7 +191,6 @@ In both of these examples, if all items are `null` values, an empty list is retu
191
191
Kotlin also provides functions that you can use to find values in collections. If a value isn't found, they return `null`
192
192
values instead of triggering an error:
193
193
194
-
*[`singleOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/single-or-null.html) looks for only one item by its exact value. If one doesn't exist or there are multiple items with the same value, returns a `null` value.
195
194
*[`maxOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/max-or-null.html) finds the highest value. If one doesn't exist, returns a `null` value.
196
195
*[`minOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/min-or-null.html) finds the lowest value. If one doesn't exist, returns a `null` value.
197
196
@@ -202,12 +201,7 @@ fun main() {
202
201
//sampleStart
203
202
// Temperatures recorded over a week
204
203
val temperatures =listOf(15, 18, 21, 21, 19, 17, 16)
205
-
206
-
// Check if there was exactly one day with 30 degrees
207
-
val singleHotDay = temperatures.singleOrNull()
208
-
println("Single hot day with 30 degrees: ${singleHotDay ?:"None"}")
209
-
// Single hot day with 30 degrees: None
210
-
204
+
211
205
// Find the highest temperature of the week
212
206
val maxTemperature = temperatures.maxOrNull()
213
207
println("Highest temperature recorded: ${maxTemperature ?:"No data"}")
@@ -224,16 +218,38 @@ fun main() {
224
218
225
219
This example uses the Elvis operator `?:` to return a printed statement if the functions return a `null` value.
226
220
227
-
> The `singleOrNull()`, `maxOrNull()`, and `minOrNull()` functions are designed to be used with collections that **don't**
221
+
> The `maxOrNull()`, and `minOrNull()` functions are designed to be used with collections that **don't**
228
222
> contain `null` values. Otherwise, you can't tell whether the function couldn't find the desired value or whether it
229
223
> found a `null` value.
230
224
>
231
225
{style="note"}
232
226
233
-
Some functions use a lambda expression to transform a collection and return `null` values if they can't
227
+
You can use the [`singleOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/single-or-null.html) function with a lambda expression to find a single item that matches a condition.
228
+
If one doesn't exist or there are multiple items that match, the function returns a `null` value:
229
+
230
+
```kotlin
231
+
funmain() {
232
+
//sampleStart
233
+
// Temperatures recorded over a week
234
+
val temperatures =listOf(15, 18, 21, 21, 19, 17, 16)
235
+
236
+
// Check if there was exactly one day with 30 degrees
237
+
val singleHotDay = temperatures.singleOrNull{ it ==30 }
238
+
println("Single hot day with 30 degrees: ${singleHotDay ?:"None"}")
> The `singleOrNull()` function is designed to be used with collections that **don't** contain `null` values.
246
+
>
247
+
{style="note"}
248
+
249
+
Some functions use a lambda expression to transform a collection and return `null` values if they can't
234
250
fulfill their purpose.
235
251
236
-
For example, to transform a collection with a lambda expression and return the first value that isn't `null`, use the
252
+
To transform a collection with a lambda expression and return the first value that isn't `null`, use the
237
253
[`firstNotNullOfOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/first-not-null-of-or-null.html) function. If no such value exists, the function returns a `null` value:
To use a lambda function to process each collection item sequentially and create an accumulated value (or return a
274
+
To use a lambda expression to process each collection item sequentially and create an accumulated value (or return a
259
275
`null` value if the collection is empty) use the [`reduceOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/reduce-or-null.html) function:
0 commit comments