W3cubDocs

/Kotlin

orEmpty

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
fun <T> Sequence<T>?.orEmpty(): Sequence<T>

Returns this sequence if it's not null and the empty sequence otherwise.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val nullSequence: Sequence<Int>? = null
println(nullSequence.orEmpty().toList()) // []

val sequence: Sequence<Int>? = sequenceOf(1, 2, 3)
println(sequence.orEmpty().toList()) // [1, 2, 3]
//sampleEnd
}

© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/or-empty.html