Kotlin Array toList Function

The toList function in Kotlin is used to create a list from an array. This function is part of the Kotlin standard library and provides a straightforward way to convert an array into a list.

Table of Contents

  1. Introduction
  2. toList Function Syntax
  3. Understanding toList
  4. Examples
    • Basic Usage
    • Using toList with Custom Types
    • Modifying the Resulting List
  5. Real-World Use Case
  6. Conclusion

Introduction

The toList function converts an array into a List collection. The resulting list is immutable, meaning that it cannot be modified. This function is useful for leveraging the features and methods of the List interface on an array.

toList Function Syntax

The syntax for the toList function is as follows:

fun <T> Array<out T>.toList(): List<T>

Parameters:

  • This function does not take any parameters.

Returns:

  • An immutable list containing the elements of the original array.

Understanding toList

The toList function creates an immutable list that contains the elements of the original array. Unlike asList, which creates a list view of the array, toList creates a separate list that does not reflect changes to the original array.

Examples

Basic Usage

To demonstrate the basic usage of toList, we will create an array of integers and convert it into a list.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val numbersList = numbers.toList()
    println("Original array: ${numbers.joinToString()}")
    println("Converted list: $numbersList")
}

Output:

Original array: 1, 2, 3, 4, 5
Converted list: [1, 2, 3, 4, 5]

Using toList with Custom Types

This example shows how to use toList with an array of custom objects.

Example

data class Person(val name: String, val age: Int)

fun main() {
    val people = arrayOf(
        Person("Ravi", 25),
        Person("Anjali", 30),
        Person("Priya", 22)
    )

    val peopleList = people.toList()
    println("Original array: ${people.joinToString()}")
    println("Converted list: $peopleList")
}

Output:

Original array: Person(name='Ravi', age=25), Person(name='Anjali', age=30), Person(name='Priya', age=22)
Converted list: [Person(name='Ravi', age=25), Person(name='Anjali', age=30), Person(name='Priya', age=22)]

Modifying the Resulting List

This example demonstrates how to handle attempts to modify the resulting list, which is immutable.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val numbersList = numbers.toList()

    try {
        numbersList.add(6)
    } catch (e: UnsupportedOperationException) {
        println("Error: ${e.message}")
    }

    println("Original array: ${numbers.joinToString()}")
    println("Immutable list: $numbersList")
}

Output:

Error: UnsupportedOperationException
Original array: 1, 2, 3, 4, 5
Immutable list: [1, 2, 3, 4, 5]

Real-World Use Case

Converting an Array to a List for Collection Operations

In real-world applications, the toList function can be used to convert an array into an immutable list to leverage the powerful collection operations available in Kotlin.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val numbersList = numbers.toList()

    val evenNumbers = numbersList.filter { it % 2 == 0 }
    println("Even numbers: $evenNumbers")
}

Output:

Even numbers: [2, 4]

Conclusion

The toList function in Kotlin is a convenient method for creating an immutable list from an array. It allows for leveraging the powerful features and methods of the List interface on an array while ensuring that the resulting list cannot be modified. By understanding and using this function, you can effectively manage array-to-list conversions and utilize collection operations in your Kotlin applications.

Comments