6 ways to add items to a list in Kotlin with examples

How to add items to a list in Kotlin:

This post will show you how to add items to a list in Kotlin. We can add a single or multiple items to a list in Kotlin. We need to use one mutable list for any list modification.

Adding a single element using add():

The add() method is used to add one single element to a list. This method is defined as below:

fun add(element: E): Boolean

It adds the argument element to the list and returns one boolean value defining whether the addition was successful or not.

Another overloaded variant of the add() method is available to insert an element at a specific index:

fun add(index: Int, e: E): Unit

It takes the index as the first parameter and inserts the element e at that index.

Let’s take a look at the example below:

fun main() {
    val givenList = mutableListOf("one", "two", "three")
    givenList.add("four")

    println(givenList)

    givenList.add(0, "zero")
    println(givenList)
}

Download it on Github

It adds two strings to the mutable list givenList one by one. The first string is added without any index and the second string is added at index 0. It prints the below output:

[one, two, three, four]
[zero, one, two, three, four]

The first addition inserts the item at the end of the list and the second addition inserts the item at the beginning.

Adding multiple elements using addAll:

The addAll method works similarly to add. We can use this method to add more than one item. Following are the definitions of this method:

public fun addAll(index: Int, elements: Collection<E>): Boolean

override fun addAll(elements: Collection<E>): Boolean

As you can see here, we can provide an index and a collection to insert it from a specific index or we can directly insert the items at the end of a list.

fun main() {
    val givenList = mutableListOf("one", "two", "three")
    givenList.addAll(0,arrayListOf("-3", "-2", "-1", "0"))
    
    println(givenList)
}

Download it on Github

This example adds the arraylist at index 0 and it modifies the mutable list givenList.

It will print the below output:

[-3, -2, -1, 0, one, two, three]

Adding items using plus() and plusElement():

By using plus() and plusElement() methods, we can add a single or multiple items to a given list. But, it doesn’t modify the original list, it creates a new list and returns that list.

fun main() {
    val givenList = mutableListOf("one", "two", "three")
    val newList = givenList.plus("four").plus("five").plus(arrayOf("six", "seven", "eight"))

    println(newList)

    val anotherList = givenList.plusElement("nine")
    println(anotherList)
}

Download it on Github

It will not change the original list givenList. So, we are defining a new variable anotherList to assign the result.

It prints the below output:

[one, two, three, four, five, six, seven, eight]
[one, two, three, nine]

Using plusAssign():

The plusAssign method modifies a mutable list. This method is similar to the plus() method, but it doesn’t return a new list. It directly changes the original list.

For example:

fun main() {
    val givenList = mutableListOf("one", "two", "three")
    givenList.plusAssign("four")
    givenList.plusAssign("five")
    givenList.plusAssign(arrayOf("six", "seven", "eight"))

    println(givenList)
}

Download it on Github

It prints:

[one, two, three, four, five, six, seven, eight]

By using the + operator:

The + operator works similarly to the plus method. It creates a new list without modifying the original list. For example:

fun main() {
    val givenList = mutableListOf("one", "two", "three")
    val newList = givenList + "four" + "five" + "six"

    println(newList)
}

Download it on Github

It gives the below output:

[one, two, three, four, five, six]

Kotlin add items to a list different ways

You might also like: