1. Introduction

Kotlin provides various ways to create lists of objects. In this tutorial, we’ll explore three common methods for creating lists in Kotlin – using listOf(), mutableListOf(), and the List() constructor.

By understanding these methods, we’ll be able to effectively manage and manipulate lists in our applications.

2. Using listOf()

The listOf() function is a convenient way to create an immutable list. It takes any number of elements as arguments and returns a list containing those elements:

data class Person(val name: String, val age: Int)
val people = listOf(
    Person("Alice", 25),
    Person("Bob", 30),
    Person("Charlie", 35)
)

In the above code snippet, we define a data class Person with name and age properties. We then create a list named people containing three Person objects. Once created, this list cannot be modified. It’s important to note that although the list is immutable, objects stored within might have mutable properties.

3. Using mutableListOf()

When we need to create a list that can be modified later, we can use the mutableListOf() function. It works similarly to listOf() but returns a mutable list instead:

data class Dog(val name: String, val age: Int)
val dogs = mutableListOf(
    Dog("Buddy", 3),
    Dog("Max", 5),
    Dog("Lucy", 2)
)

In the above code snippet, we define a data class Dog with name and age properties. We create a mutable list named dogs containing three Dog objects. Since it’s mutable, we can add, remove, or modify elements in the list:

dogs.add(Dog("Daisy", 4))
println(dogs) // [Dog("Buddy", 3), Dog("Max", 5), Dog("Lucy", 2), Dog("Daisy", 4)]

4. Using the List Constructor

Kotlin also provides a List() constructor-like function that allows us to create lists. This constructor takes a size parameter and an initialization lambda function that gives us the current index and expects an object.

Let’s see a simple example:

data class Car(val model: String, val price: Int)
val cars = List(3) { index ->
    Car("Car ${index + 1}", (index + 1) * 100)
}

Here, we define a data class Car with model and price properties. We create a list named cars of size 3. The lambda function passed to the constructor initializes each element of the list with a Car object, where model is based on index and price is calculated based on index.

5. Conclusion

In this article, we explored three methods for creating lists of objects in Kotlin. We learned that listOf() creates an immutable list, mutableListOf() creates a mutable list, and the List() constructor allows us to create lists with custom initialization logic.

Depending on our needs, we can choose the appropriate method to create and manage lists in our Kotlin projects. listOf() and mutableListOf() are great for simple scenarios, while the List() constructor provides more flexibility for complex initialization logic.

As always, the code for the examples is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.