Kotlin Lists — Complete Guide

1. Why Use Lists in Kotlin?

In Kotlin, Lists are used to store ordered collections of elements that can be accessed by index. Unlike arrays, lists are more flexible and come in two main types:

Lists are widely used in Android development for displaying collections such as items in a RecyclerView, menu options, or network responses.

Example:

// Example: List of fruits
val fruits = listOf("Apple", "Banana", "Cherry")
println(fruits[1]) // Output: Banana
      

2. How to Declare and Use Lists in Kotlin (with 5 Examples)

Example 1 — Using listOf() (Immutable List)

// Basic declaration
val colors = listOf("Red", "Green", "Blue")

// Access elements
println(colors[0])   // Red
println(colors.size) // 3

// Iterate
for (color in colors) println(color)
    

// With explicit type
val numbers: List = listOf(1, 2, 3, 4, 5)
println(numbers.joinToString(" - ")) // 1 - 2 - 3 - 4 - 5
    
Example 2 — Using mutableListOf() (Mutable List)

// Create a mutable list
val cities = mutableListOf("Lahore", "Karachi", "Islamabad")

// Add or remove elements
cities.add("Quetta")
cities.remove("Karachi")

println(cities) // [Lahore, Islamabad, Quetta]
    

// Another way with explicit type
val scores: MutableList = mutableListOf(10, 20, 30)
scores[1] = 25
scores.add(40)
println(scores) // [10, 25, 30, 40]
    
Example 3 — Using emptyList() and mutableListOf()

// Empty immutable list
val empty = emptyList()
println(empty.isEmpty()) // true
    

// Empty mutable list
val mutable = mutableListOf()
mutable.add("Talha")
mutable.add("Abbas")
println(mutable) // [Talha, Abbas]
    
Example 4 — Using List() Constructor

// Create a list using lambda
val squares = List(5) { it * it }
println(squares) // [0, 1, 4, 9, 16]
    

// Another example
val doubled = List(4) { i -> i * 2 }
println(doubled) // [0, 2, 4, 6]
    
Example 5 — Converting Between Lists and Arrays

// List to Array
val list = listOf("A", "B", "C")
val arr = list.toTypedArray()
println(arr.joinToString()) // A, B, C
    

// Array to List
val arr2 = arrayOf(10, 20, 30)
val newList = arr2.toList()
println(newList) // [10, 20, 30]
    

3. When and When Not to Use Lists?

✅ When to Use Lists:
🚫 When Not to Use Lists:

4. Top 10 Interview Questions (With Answers)

  1. Q1: Difference between List and MutableList?
    Ans: List is read-only; MutableList supports adding, removing, or updating elements.
  2. Q2: Can a list contain duplicates?
    Ans: Yes. Lists can have duplicate elements.
  3. Q3: How to create an empty list?
    Ans: Use emptyList<T>() for immutable or mutableListOf<T>() for mutable.
  4. Q4: Convert a list to array?
    Ans: list.toTypedArray()
  5. Q5: What happens if you modify an immutable list?
    Ans: Compilation error — List doesn’t allow modification.
  6. Q6: How to iterate through a list?
    Ans: Use for loop or forEach:
    
    val items = listOf("A", "B", "C")
    items.forEach { println(it) }
            
  7. Q7: Check if an element exists?
    Ans: Use in or contains():
    
    if ("Apple" in fruits) println("Found")
            
  8. Q8: Add element at a specific index?
    Ans: add(index, element):
    
    val names = mutableListOf("A", "B", "C")
    names.add(1, "Z")
    println(names) // [A, Z, B, C]
            
  9. Q9: Remove duplicates?
    Ans: Use distinct():
    
    val nums = listOf(1, 2, 2, 3)
    println(nums.distinct()) // [1, 2, 3]
            
  10. Q10: Sort or reverse a list?
    Ans: Use sorted(), sortedDescending(), or reversed():
    
    val list = listOf(5, 2, 8, 1)
    println(list.sorted())           // [1, 2, 5, 8]
    println(list.sortedDescending()) // [8, 5, 2, 1]
            

Array vs List in Kotlin — Why List is Preferred

1. Are Arrays and Lists Both Collections?

✅ Yes! In Kotlin, both Arrays and Lists are part of the collection system, but they differ in behavior and flexibility.

Feature Array List
TypeFixed-size collectionDynamic collection (MutableList)
MutabilityCan change elements but not sizeImmutable (List) or mutable (MutableList)
Resizing❌ Not allowed✅ Allowed
Functional OperationsLimited built-in functionsPowerful extensions like filter(), map(), find()
UsageUsed for fixed size and type (performance-critical code)Used in modern Kotlin & Android apps for dynamic data

2. Why List is Better for Filtering and Searching

Kotlin’s List is built on Collection<T>, offering a rich API for filtering, mapping, and searching efficiently.

Example — Filtering a List


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

Example — Searching in a List


val names = listOf("Talha", "Ali", "Ahmed", "Zain")
val found = names.find { it.startsWith("A") }
println(found) // Ahmed
      

Arrays don’t natively support these operations — Kotlin internally converts them to Lists for functions like filter() or map(). Hence, Lists are preferred for cleaner and faster high-level code.

3. Final Comparison Summary

  • Array: Best for performance-critical, fixed-size data.
  • 💡 List: Best for flexible, filterable, and dynamic data in Android.
Bonus Tip 💡

Use MutableList in RecyclerViews or APIs where data changes. Use List when data is static and should remain read-only.