What is the function of emptyList in Kotlin

Related searches

Why there is such emptyList constructor in Kotlin? It was a immutable List, so there is no way to add or remove its elements and it was empty! So, what is the function of this emptyList?

The emptyList is not a constructor but a function that returns and immutable empty list implementation.

The main reason such function exists is to save allocations. Since emptyList returns the same singleton instance every time it is called so one can use it in allocation free manner. Note that the very same object is returned regardless of the element type i.e. emptyList<String>() === emptyList<Int>() is true.

IMHO emptyList also reads a bit better than listOf when used as e.g. a default parameter value:

data class Person(val friends:List<Person> = emptyList())

emptyList, Functions and Lambdas. Functions another way to create an empty list, // type parameter is inferred from the expected type val other: List<Int> = emptyList() The emptyList is not a constructor but a function that returns and immutable empty list implementation. The main reason such function exists is to save allocations. Since emptyList returns the same singleton instance every time it is called so one can use it in allocation free manner. Note that the very same object is returned regardless of the element type i.e. emptyList<String> () === emptyList<Int> () is true.

To address

Actually i want to know why people using emptyList? What for?

more directly: you need it e.g. to pass to methods taking lists, or when you have a list which is empty in one branch of your code and not another:

val commandLineOptions: List<String> = when {
    x > 0 -> listOf(...)
    else -> emptyList()
}

runCommand(command, commandLineOptions)

listOf, isEmpty() is ${list.isEmpty()}") // true // another way to create an empty list, // type parameter is inferred from the expected type val other: List<Int> = emptyList() This article explores different ways to initialize an empty List in Kotlin. There are several ways to initialize an empty list as discussed below: 1. listOf() function. If you need an immutable empty list instance, you can use listOf() function as shown below.

orEmpty, orEmpty(): Collection<T>. Returns this Collection if it's not null and the empty list otherwise. import kotlin.test.* fun main(args: Array<String>) { //sampleStart val� This article explores different ways to initialize list in Kotlin in a single line. 1. listOf() function The listOf() function returns an immutable list of given elements which does not permit addition or removal of elements.

Actually,even if you declare an empty list,you can still add elements to this empty list by using the "+", because Kotlin override some operators in its libraries:

var l = emptyList<Int>() 
print(l + 1) // output [1]

How to initialize an empty List in Kotlin, There are several ways to initialize an empty list as discussed below If you need an immutable empty list instance, you can use `listOf()` function Returns the first entry yielding the smallest value of the given function or null if there are no entries.

How to check for a null or empty List in Kotlin, 1. isNullOrEmpty() function. From Kotlin 1.3 onwards, the recommended approach is to use isNullOrEmpty() method to check for an empty or null list in� Kotlin is a statically typed language, hence, functions play a great role in it. We are pretty familiar with function, as we are using function throughout the examples. Function is declared with the keyword “fun”. Like any other OOP, it also needs a return type and an option argument list.

Kotlin™ is protected under the Kotlin Foundation and licensed under the Apache 2 license. Supported and developed by JetBrains Supported and developed by JetBrains

Function references in Unit-returning functions. In Kotlin 1.4, you can use callable references to functions returning any type in Unit-returning functions. Before Kotlin 1.4, you could only use lambda arguments in this case. Now you can use both lambda arguments and callable references.

Comments
  • Are you asking why someone would need an empty list? Or why there is a named function to create one such? Or why it is a function and not a constant value?
  • FWIW, Java also has Collections.emptyList()
  • Actually i want to know why people using emptyList? What for?
  • In Java you even have Collections.singletonList().
  • And the reason it has to be a function (instead of just exposing the underlying singleton object instance directly) is that it couldn't be generic otherwise: You want to use the same instance for empty String lists and empty BigDecimal lists. This works because you are never going to get any objects out of an empty list, so it is perfectly type-safe that way.
  • I don't think this the correct use of the term "singleton". It has only one of multiple things in common with singletons. I think this answer could be improved by avoiding possible confusions with singletons (e.g. that every encapsulated const/final variable is a singleton).
  • Just one good example. It's also common for unit tests when you need to check for behaviour when inputs don't contain any elements...
  • You're not adding to the original empty list, you're using it as the starting point and creating a new list with whatever you added to it. In practice, it's similar, but still an important distinction.