Kotlin Collections


Kotlin Collections

Collections are a general concept for most programming languages. A collection usually consists of several objects of the same type, and the objects in a collection are called elements or items.

Kotlin Standard Library provides a comprehensive set of tools for managing archives. The following collection types are relevant to the code:

  • Kotlin List - A list is an order collection that has access to components by index. A list can have multiple components.
  • Kotlin set - A set of unique elements with non-repetitive objects.
  • Kotlin Map - A map (or dictionary) is a set of key-value pairs. The keys are unique, each mapped to an exact value.

Kotlin Collection Types

Kotlin offers the following types of collections:

  • Collection or Immutable (Non-Changeable) collection
  • Mutable (Changeable) collection

Immutable Collection

Calling an immutable collection or a collection interface provides read-only methods, i.e., once a collection has been created, we cannot change it because there is no way to change the created object.

Collection Types Methods of Immutable Collection
List listOf()
listOf()
Map mapOf()
SetsetOf()

It provides methods for reading and writing interchangeable collections.

Collection Types Methods of mutable Collection
List ArrayList()
arrayListOf()
mutableListOf()
Map HashMap
hashMapOf()
mutableMapOf()
Set hashSetOf()
mutableSetOf()

Note: Altering a mutable collection doesn't require it to be a var.