makeger.blogg.se

Kotlin empty list
Kotlin empty list











This interface enables collections to be represented as a sequence of elements (which can be iterated over, naturally). The Kotlin Iterable interface is at the top of the collections class hierarchy. Note that the addition, removal, or replacement of an element in an immutable collection is possible via operator functions (we'll get to that shortly), but these will end up creating a new collection.

kotlin empty list

Immutable collections cannot be modified and don't have these helper methods. A mutable collection provides us with the ability to modify a collection by either adding, removing, or replacing an element. Kotlin's collections give us the ability to achieve a lot with just a little code-unlike in Java, which seems to need a lot of code to achieve a little! Kotlin has two variants of collections: mutable and immutable. (If you want a refresher on arrays in Kotlin, kindly visit the first tutorial in this series.) In this section, we'll learn about the List, Set, and Map collections in Kotlin. To really understand the collections API in Kotlin, you'll need to be familiar with these basic classes and interfaces in Java. You can't see the implementation source code in Kotlin because the collections are actually implemented by the standard Java Collections such as ArrayList, Maps, HashMap, Sets, HashSet, List, and so on. You should note that these interfaces are linked to their implementation at compile time. (We'll discuss interfaces in Kotlin in a future post.)

kotlin empty list

Kotlin provides its collections API as a standard library built on top of the Java Collections API.

kotlin empty list

We can retrieve, store, or organize the objects in a collection. Collections are used to store groups of related objects in memory.













Kotlin empty list