I want to check if these collections are empty or not.
Why can't you just use the method isEmpty()?
print(occupations.isEmpty()) // >>> false
print(shoppingList.isEmpty()) // >>> false
print(favoriteGenres.isEmpty()) // >>> false
Anyway, if you really want to declare an empty collection, you can do it like this:
val emptyList = listOf<String>()
val emptySet = setOf<String>()
val emptyMap = mapOf<String, String>()
OR
val emptyList = emptyList<String>()
val emptySet = emptySet<String>()
val emptyMap = emptyMap<String, String>()
Let me take a look under the hood. A method listOf()called without arguments has the following implementation:
@kotlin.internal.InlineOnly
public inline fun <T> listOf(): List<T> = emptyList()
It is easy to see that it simply calls another method - emptyList():
public fun <T> emptyList(): List<T> = EmptyList
EmptyList:
internal object EmptyList : List<Nothing>, Serializable, RandomAccess {
}
, , ( @brescia123) : List, , .