orEmpty

inline actual fun <reified T> Array<out T>?.orEmpty(): Array<out T> (source)

Returns the array if it's not null, or an empty array otherwise.



fun main(args: Array<String>) {
//sampleStart
val nullArray: Array<Any>? = null
println(nullArray.orEmpty().contentToString()) // []

val array: Array<Char>? = arrayOf('a', 'b', 'c')
println(array.orEmpty().contentToString()) // [a, b, c]
//sampleEnd
}
inline fun <T> Collection<T>?.orEmpty(): Collection<T> (source)

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

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val nullCollection: Collection<Any>? = null
println(nullCollection.orEmpty()) // []

val collection: Collection<Char>? = listOf('a', 'b', 'c')
println(collection.orEmpty()) // [a, b, c]
//sampleEnd
}
inline fun <T> List<T>?.orEmpty(): List<T> (source)

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

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val nullList: List<Any>? = null
println(nullList.orEmpty()) // []

val list: List<Char>? = listOf('a', 'b', 'c')
println(list.orEmpty()) // [a, b, c]
//sampleEnd
}
inline fun <K, V> Map<K, V>?.orEmpty(): Map<K, V> (source)

Returns the Map if its not null, or the empty Map otherwise.

inline fun <T> Set<T>?.orEmpty(): Set<T> (source)

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