List and MutableList

List is one of the most commonly used collection data types. It is an implementation of the Collection interface used to work with a group of ordered data.

The data in a list may be ordered based on when it was added (like if we add 3 after 4 to an Int List, then 4 will appear in the list before 3, much like an array) or may even be ordered based on other ordering algorithms.

As we mentioned earlier, Kotlin distinguishes between mutable and read-only collection types; so, the List interface, which is immutable, contains only the read-only functions, as follows:

  • fun get(index: Int):E: This method is used to get an element from the list at the given index.
  • fun indexOf(element: @UnsafeVariance E):Int: This method is used ...

Get Functional Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.