Kotlin Lists

In the previous chapter we talked about the arrays or arrays and we saw all the potential that it had, but one of the biggest drawbacks was the limitation when defining them, since we had to know in advance the size of said array.

 Types of lists 

The collections can be classified into two large groups, mutable and immutable. That is, those that can be edited (mutable) and those that are only readable (immutable).

 Immutable lists 

The declaration of an immutable list would be like that.

 val readOnly: List < string> = listOf ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

There are several useful functions to work with them.

 val readOnly: List < string> = listOf ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

    readOnly.size // Show the size of the list
    readOnly.get (5) // Returns the value of position 5
    readOnly.first () // Returns the first value
    readOnly.last () // Returns the last value
    println (readOnly) // [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

As you can see we can work completely with them, except for adding more elements. We can also filter using the iterator pattern, which is a mechanism to access the elements of the list, for example .first() or .last()

val a = readOnly.filter {it == "Monday" || it == "Juernes"}

The .filter allows us to filter in the list through one or several conditions that we put. For this we call it (iterator) and look in the list, if it contains the word "Monday" or "Juernes". In this case, you will only paint "Monday".

 Mutable lists 

Now we have to talk about the most interesting, mutable lists, which have all the above, but also gives us the ability to fill in the list as we need it, the only drawback is that more inefficient memory.

var mutableList: MutableList < string> = mutableListOf ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 


    println (readOnly) // [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

    mutableList.add ("Sunday")

The definition is very similar, through the mutableListOf function. Now if you notice I have added from Monday to Saturday what will be painted, then, using mutableList.add I added Sunday. By default the values ​​will be added in the last position, but we can add the index in which we want to write our value.

mutableList.add (0, "Week:")

With this, if we painted the list, it would show us the following.

[Week:, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

Now we have to be careful with a mutable list for the simple fact that it could be empty or contain a null. A null is a null value in one of its positions, which, if we access it and try to operate, the application will be broken, it will crash. For this, we have some functions that will allow us to work as an immutable list, but with security.

 var mutableList: MutableList < string> = mutableListOf ()
    mutableList.none () // We return a true if the list is empty
    mutableList.firstOrNull () // We will return the first field, and if there is not, a null.
    mutableList.elementAtOrNull (2) // The element in index 2, if not, will return a null
    mutableList.lastOrNull () // Last value in the list or null

In this example I have instantiated a list with no value, so it is empty. With all these methods we will recover a null (except the first one that will give true) and the application will continue running, if otherwise we had put a .first() we would have passed the following. We must be very careful when working with data that can change. **Touring lists As we did in the chapter of the arrays, here we also have (very similar) ways to go through the lists. This would be the simplest way, and it would return the contents of each of the values ​​in the list.

  for (item in mutableList) {
        print (item)

If we also need to know the position of each of the values ​​we can use the function .withIndex that allows us to generate 2 variables, the first will be the position and the second the content.

  for ((index, item) in mutableList.withIndex()) {
        println ("The position $ index contains $ item")

For the last example, we will use .forEach, a function that will do one thing for each position, for example, to paint the value as the previous for. Unlike them, we do not have a variable with the content (see index and item) but we access it with the iterator, in this case we simply have to put it. In this example, let's imagine that we want to add two points to the days of the week ":" so we are going to create a new list (mutable) and we will fill in the list through the forEach.

val mutableList: MutableList < string> = mutableListOf ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 

    val newListEmpty: MutableList < string> = mutableListOf ()

    mutableList.forEach {
        newListEmpty.add (it + ":")

    print (newListEmpty)

Leave a Reply