</>
Skip to content
Kotlin lessons (20/33)

Kotlin — Collections

Lists

fun main() {
    // Immutable list
    val nums = listOf(1, 2, 3, 4, 5)
    println(nums)        // [1, 2, 3, 4, 5]
    println(nums[0])     // 1
    println(nums.size)   // 5

    // Mutable list
    val mutable = mutableListOf(1, 2, 3)
    mutable.add(4)
    mutable.removeAt(0)
    println(mutable)     // [2, 3, 4]
}

Sets

fun main() {
    val set = setOf(1, 2, 3, 2, 1)
    println(set)          // [1, 2, 3]
    println(set.contains(2)) // true

    val mutableSet = mutableSetOf(1, 2, 3)
    mutableSet.add(4)
    mutableSet.remove(1)
    println(mutableSet)   // [2, 3, 4]
}

Maps

fun main() {
    val map = mapOf("Alice" to 30, "Bob" to 25)
    println(map["Alice"])    // 30
    println(map.getOrDefault("Charlie", 0)) // 0

    val mutable = mutableMapOf("Alice" to 30)
    mutable["Bob"] = 25
    mutable.remove("Alice")
    println(mutable)         // {Bob=25}
}

Collection operations

fun main() {
    val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    // Filter
    val evens = nums.filter { it % 2 == 0 }
    println(evens) // [2, 4, 6, 8, 10]

    // Map
    val doubled = nums.map { it * 2 }
    println(doubled) // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

    // Reduce
    val sum = nums.reduce { acc, i -> acc + i }
    println(sum) // 55

    // Fold
    val product = nums.fold(1) { acc, i -> acc * i }
    println(product)

    // Any / All / None
    println(nums.any { it > 5 })  // true
    println(nums.all { it > 0 })  // true
    println(nums.none { it > 10 }) // true
}

Chaining operations

fun main() {
    val result = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .filter { it % 2 == 0 }
        .map { it * it }
        .take(3)
        .sum()

    println(result) // 20 (4 + 16 + 36)
}

Sequences (lazy evaluation)

fun main() {
    val seq = sequence {
        var i = 0
        while (true) {
            yield(i++)
        }
    }

    val firstTen = seq.take(10).toList()
    println(firstTen) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}

Mini Practice

Write Kotlin code that:

  1. Creates and manipulates a MutableList
  2. Chains filter, map, and reduce operations
  3. Uses a sequence for lazy evaluation
  4. Groups a list of words by their first letter

Up Next

In the next lesson, you'll learn about Interfaces — defining contracts in Kotlin.

Related Topics

Frequently Asked Questions about Collections

What is Collections in Kotlin?

Collections is a fundamental concept in Kotlin. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Collections?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Collections.

Why is Collections important in Kotlin?

Collections is essential for Kotlin development. Understanding this concept will help you write better code and solve real-world problems more effectively.