Kotlin List

Kotlin List is one of the three Collections (List, Set, Map). List is a collection of elements whose position in the collection is preserved, whereas in Set or Map, there is no fixed position/index for an element. Hence, elements in a list can be accessed using index.

List is immutable, so, you cannot update individual elements of list or add items to the original list.

But if you want to create a list that could be mutable, Kotlin provides MutableList<T> class to define a mutable list.

In this tutorial, we shall learn with examples on how to access and modify items in a list, and how to convert it to other Collection types.

Kotlin List – Initialize

To initialize Kotlin List, use mutableListOf(vararg items : T) method. The method returns a MutableList<T>.

In the following example,

listA is an immutable list of type List<String>, since each of the element in the list is of type String.

listB is a mutable list of type MutableList<String>.

Kotlin List – Get Element

List.get() is used to read elements of a list using index.

You can also use array style accessing of elements using square brackets [].

In the above example, we have used square brackets to read element present at index 0 and used get() method to read element at index 2.

Kotlin – Add Element to List

Since we cannot modify a List<T>, we shall learn how to add an element to a mutable list, MutableList<T>.

To append item to the list, use MutableList.add(item : T) method.

The item is added to the end of list.

Kotlin – Check if List Contains an Element

To check if an element is present in the list, use List.contains() method on the list. contains() return true if the element is present, else false is returned.

As the element is present in the list, b is set to true and hence the print statement with the string "Program is present in the list." is executed.

Kotlin – Find Element in List

You can use List.find() function to find the first element that satisfies a predicate provided to the find() method. Element of the list can be accessed using it. Observe the following example program and output, and you shall follow what we are talking about this find() function.

In the following example, we find the first element that contains string “am”. find() method returns the first element that satisfies the condition posed by predicate.

Kotlin – Filter Elements of List

List.filter() function can be used to filter elements of list based on a predicate. it in the predicate represents each element.

In the following example, we filter a list for the elements containing “am”. filter method returns new list with items filtered based on the predicate.

The filtered output contains elements that satisfy the predicate we passed to filter() function.

Kotlin List – For Loop

To iterate over all the elements of a list, for loop can be used. Following is an example to print all elements of a list using for loop.

Conclusion

In this Kotlin TutorialKotlin List, we have learnt to initialize a List with items, access them, filter them based on a predicate, find an element based on a predicate, etc.