Kotlin List Interface
Kotlin List is an interface and generic collection of elements. The List interface inherits form Collection<T> class. It is immutable and its methods supports only read functionalities.
To use the List interface we need to use its function called listOf(), listOf<E>().
The elements of list follow the sequence of insertion order and contains index number same as array.
List Interface Declaration
Function of Kotlin List Interface
There are several functions are available in the List interface. Some functions of List interface are mention below.
Kotlin List Example 1
Let's see an example of list using listOf() function.
Ajay Vijay Prakash
Kotlin List Example 2
In the listOf() function we can pass the different types of data at the same time. List can also traverse the list using index range.
1 2 3 Ajay Vijay Prakash 1 2 3 Ajay Vijay Prakash
Kotlin List Example 3
For more specific we can provide the generic types of list such as listOf<Int>(), listOf<String>(), listOf<Any>() Let's see the example.
print int list 1 2 3 print string list Ajay Vijay Prakash print any list 1 2 3 Ajay Vijay Prakash
Kotlin List Example 4
Let's see the use of different function of Kotlin list interface using listOf<T>() function.
Ajay Vijay Prakash Vijay Rohan Ajay 1 3 5 true true [Prakash, Vijay] false [Vijay, Prakash, Vijay, Rohan] [Ajay, Vijay, Prakash]
The limitation of List interface is that it is immutable. It cannot add more elements in list after its declaration. To solve this limitation Collection framework provide mutable list.