Returns this Collection if it's not null
and the empty list otherwise.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
val nullCollection: Collection<Any>? = null
println(nullCollection.orEmpty())
val collection: Collection<Char>? = listOf('a', 'b', 'c')
println(collection.orEmpty())
}
Target: JVMRunning on v.2.1.20
Returns this List if it's not null
and the empty list otherwise.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
val nullList: List<Any>? = null
println(nullList.orEmpty())
val list: List<Char>? = listOf('a', 'b', 'c')
println(list.orEmpty())
}
Target: JVMRunning on v.2.1.20
Returns the Map if its not null
, or the empty Map otherwise.
Since Kotlin
1.0Samples
import kotlin.test.*
import java.util.*
fun main() {
val nullMap: Map<String, Any>? = null
println(nullMap.orEmpty())
val map: Map<Char, Int>? = mapOf('a' to 1, 'b' to 2, 'c' to 3)
println(map.orEmpty())
}
Target: JVMRunning on v.2.1.20
Returns this Set if it's not null
and the empty set otherwise.
Since Kotlin
1.0
Returns the array if it's not null
, or an empty array otherwise.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
val nullArray: Array<Any>? = null
println(nullArray.orEmpty().contentToString())
val array: Array<Char>? = arrayOf('a', 'b', 'c')
println(array.orEmpty().contentToString())
}
Target: JVMRunning on v.2.1.20
Returns the array if it's not null
, or an empty array otherwise.
Since Kotlin
1.1Samples
import kotlin.test.*
fun main() {
val nullArray: Array<Any>? = null
println(nullArray.orEmpty().contentToString())
val array: Array<Char>? = arrayOf('a', 'b', 'c')
println(array.orEmpty().contentToString())
}
Target: JVMRunning on v.2.1.20
Returns the array if it's not null
, or an empty array otherwise.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
val nullArray: Array<Any>? = null
println(nullArray.orEmpty().contentToString())
val array: Array<Char>? = arrayOf('a', 'b', 'c')
println(array.orEmpty().contentToString())
}
Target: JVMRunning on v.2.1.20
Returns the array if it's not null
, or an empty array otherwise.
Since Kotlin
1.3Samples
import kotlin.test.*
fun main() {
val nullArray: Array<Any>? = null
println(nullArray.orEmpty().contentToString())
val array: Array<Char>? = arrayOf('a', 'b', 'c')
println(array.orEmpty().contentToString())
}
Target: JVMRunning on v.2.1.20