Kotlin Lists

Introduction

Lists in Kotlin are ordered collections of elements that can contain duplicates.

Kotlin provides two types of lists: List for immutable lists and MutableList for mutable lists.

Lists are commonly used for storing and manipulating collections of related data.

This chapter will cover how to create and manipulate both immutable and mutable lists, along with common operations that can be performed on lists.

Immutable Lists

An immutable list cannot be modified after it is created. You can use the listOf function to create an immutable list.

Creating an Immutable List

Syntax

val list: List<Type> = listOf(elements)

Example

fun main() {
    val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
    println(fruits)
}

Explanation:

  • val fruits: List<String> = listOf("Apple", "Banana", "Cherry"): Creates an immutable list of strings.

Output:

[Apple, Banana, Cherry]

Mutable Lists

A mutable list can be modified after it is created. You can use the mutableListOf function to create a mutable list.

Creating a Mutable List

Syntax

val list: MutableList<Type> = mutableListOf(elements)

Example

fun main() {
    val fruits: MutableList<String> = mutableListOf("Apple", "Banana")
    fruits.add("Cherry")
    println(fruits)
}

Explanation:

  • val fruits: MutableList<String> = mutableListOf("Apple", "Banana"): Creates a mutable list of strings.
  • fruits.add("Cherry"): Adds an element to the mutable list.

Output:

[Apple, Banana, Cherry]

Common List Operations

Kotlin provides various operations that can be performed on lists, such as accessing elements, iterating through lists, adding/removing elements, and more.

Accessing Elements

You can access elements in a list using the index operator ([]).

Example

fun main() {
    val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
    println(fruits[0]) // Prints "Apple"
    println(fruits[2]) // Prints "Cherry"
}

Output:

Apple
Cherry

Iterating Through a List

You can iterate through a list using a for loop.

Example

fun main() {
    val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
    for (fruit in fruits) {
        println(fruit)
    }
}

Output:

Apple
Banana
Cherry

Adding and Removing Elements (Mutable List)

You can add and remove elements from a mutable list using various methods.

Example

fun main() {
    val fruits: MutableList<String> = mutableListOf("Apple", "Banana")
    fruits.add("Cherry")
    println(fruits) // Prints "[Apple, Banana, Cherry]"

    fruits.remove("Banana")
    println(fruits) // Prints "[Apple, Cherry]"

    fruits.add(1, "Blueberry")
    println(fruits) // Prints "[Apple, Blueberry, Cherry]"
}

Output:

[Apple, Banana, Cherry]
[Apple, Cherry]
[Apple, Blueberry, Cherry]

Filtering

Filtering returns a list containing only elements that match a given predicate.

Example

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val evenNumbers = numbers.filter { it % 2 == 0 }
    println(evenNumbers) // Prints "[2, 4]"
}

Output:

[2, 4]

Mapping

Mapping transforms each element in a list based on a given function.

Example

fun main() {
    val numbers = listOf(1, 2, 3)
    val squares = numbers.map { it * it }
    println(squares) // Prints "[1, 4, 9]"
}

Output:

[1, 4, 9]

Sorting

You can sort a list using the sorted or sortedBy functions.

Example

fun main() {
    val numbers = listOf(3, 1, 4, 1, 5, 9)
    val sortedNumbers = numbers.sorted()
    println(sortedNumbers) // Prints "[1, 1, 3, 4, 5, 9]"
}

Output:

[1, 1, 3, 4, 5, 9]

Finding Elements

You can find elements in a list using the find or firstOrNull functions.

Example

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val firstEven = numbers.find { it % 2 == 0 }
    println(firstEven) // Prints "2"

    val firstOdd = numbers.firstOrNull { it % 2 != 0 }
    println(firstOdd) // Prints "1"
}

Output:

2
1

Example Program with Lists

Here is an example program that demonstrates various aspects of using lists in Kotlin:

fun main() {
    // Immutable list
    val fruits: List<String> = listOf("Apple", "Banana", "Cherry")
    println(fruits)

    // Mutable list
    val mutableFruits: MutableList<String> = mutableListOf("Apple", "Banana")
    mutableFruits.add("Cherry")
    println(mutableFruits)

    // Accessing elements
    println(fruits[0])
    println(fruits[2])

    // Iterating through a list
    for (fruit in fruits) {
        println(fruit)
    }

    // Adding and removing elements (mutable list)
    mutableFruits.remove("Banana")
    println(mutableFruits)
    mutableFruits.add(1, "Blueberry")
    println(mutableFruits)

    // Filtering
    val numbers = listOf(1, 2, 3, 4, 5)
    val evenNumbers = numbers.filter { it % 2 == 0 }
    println(evenNumbers)

    // Mapping
    val squares = numbers.map { it * it }
    println(squares)

    // Sorting
    val sortedNumbers = numbers.sorted()
    println(sortedNumbers)

    // Finding elements
    val firstEven = numbers.find { it % 2 == 0 }
    println(firstEven)
    val firstOdd = numbers.firstOrNull { it % 2 != 0 }
    println(firstOdd)
}

Output:

[Apple, Banana, Cherry]
[Apple, Banana, Cherry]
Apple
Cherry
Apple
Banana
Cherry
[Apple, Cherry]
[Apple, Blueberry, Cherry]
[2, 4]
[1, 4, 9, 16, 25]
[1, 2, 3, 4, 5]
2
1

Conclusion

In this chapter, you learned about lists in Kotlin, including how to create and manipulate immutable and mutable lists. You also learned about common list operations such as accessing elements, iterating through lists, adding/removing elements, filtering, mapping, sorting, and finding elements. Understanding and using lists effectively is crucial for writing robust and maintainable Kotlin programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top