How do I convert arrays to lists and vice versa in Kotlin?

In Kotlin, you can convert arrays to lists and lists to arrays using standard library functions.

Array to List

Use toList():

val array = arrayOf("a", "b", "c")

val list: List<String> = array.toList()

println(list) // [a, b, c]

If you want a mutable list, use toMutableList():

val array = arrayOf("a", "b", "c")

val mutableList: MutableList<String> = array.toMutableList()

mutableList.add("d")

println(mutableList) // [a, b, c, d]

List to Array

Use toTypedArray():

val list = listOf("a", "b", "c")

val array: Array<String> = list.toTypedArray()

println(array.contentToString()) // [a, b, c]

Primitive Arrays

Kotlin has special primitive array types like IntArray, DoubleArray, and BooleanArray.

IntArray to List

val intArray = intArrayOf(1, 2, 3)

val list: List<Int> = intArray.toList()

println(list) // [1, 2, 3]

List<Int> to IntArray

Use toIntArray():

val list = listOf(1, 2, 3)

val intArray: IntArray = list.toIntArray()

println(intArray.contentToString()) // [1, 2, 3]

Other primitive conversions work similarly:

val doubles: List<Double> = listOf(1.1, 2.2, 3.3)
val doubleArray: DoubleArray = doubles.toDoubleArray()

val booleans: List<Boolean> = listOf(true, false)
val booleanArray: BooleanArray = booleans.toBooleanArray()

Important Note: asList()

For object arrays, you can also use asList():

val array = arrayOf("a", "b", "c")

val list = array.asList()

The difference is:

val array = arrayOf("a", "b", "c")

val copiedList = array.toList()
val backedList = array.asList()
  • toList() creates a new list copy.
  • asList() returns a list backed by the original array.

Example:

val array = arrayOf("a", "b", "c")
val list = array.asList()

array[0] = "z"

println(list) // [z, b, c]

So in most cases, use:

array.toList()
list.toTypedArray()

And for primitive types:

intArray.toList()
list.toIntArray()

How do I sort a list of values in Kotlin?

In Kotlin, you usually sort a list with sorted():

val numbers = listOf(5, 2, 8, 1)

val sortedNumbers = numbers.sorted()

println(sortedNumbers) // [1, 2, 5, 8]

sorted() returns a new sorted list and does not change the original list.

For descending order, use sortedDescending():

val numbers = listOf(5, 2, 8, 1)

val sortedDescending = numbers.sortedDescending()

println(sortedDescending) // [8, 5, 2, 1]

For objects, sort by a property with sortedBy():

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

val people = listOf(
    Person("Alice", 30),
    Person("Bob", 25),
    Person("Charlie", 35)
)

val sortedByAge = people.sortedBy { it.age }

println(sortedByAge)
// [Person(name=Bob, age=25), Person(name=Alice, age=30), Person(name=Charlie, age=35)]

If you have a mutable list and want to sort it in place, use sort():

val numbers = mutableListOf(5, 2, 8, 1)

numbers.sort()

println(numbers) // [1, 2, 5, 8]

Quick summary:

  • sorted() — returns a new ascending list
  • sortedDescending() — returns a new descending list
  • sortedBy { ... } — sorts by a property
  • sort() — sorts a mutable list in place

How do I check if a collection is empty or contains an element in Kotlin?

In Kotlin, use isEmpty() / isNotEmpty() to check whether a collection has elements, and use in, contains(), or map-specific methods to check contents.

val names = listOf("Alice", "Bob")

println(names.isEmpty())     // false
println(names.isNotEmpty())  // true

println("Alice" in names)    // true
println("Charlie" !in names) // true

For lists and sets:

val numbers = setOf(1, 2, 3)

if (numbers.isNotEmpty()) {
    println("The set has elements")
}

if (2 in numbers) {
    println("The set contains 2")
}

For maps, check keys or values explicitly:

val ages = mapOf(
    "Alice" to 25,
    "Bob" to 30
)

println(ages.isEmpty())              // false
println(ages.containsKey("Alice"))   // true
println(ages.containsValue(30))      // true
println("Bob" in ages)               // true, checks keys

You can also use contains():

val items = listOf("Book", "Pen")

println(items.contains("Book")) // true

In short:

  • collection.isEmpty() checks if it has no elements
  • collection.isNotEmpty() checks if it has at least one element
  • element in collection checks if an element exists
  • element !in collection checks if an element does not exist
  • map.containsKey(key) checks for a key
  • map.containsValue(value) checks for a value

How do I access elements safely in Kotlin collections?

In Kotlin, you can access collection elements safely by using functions that return null instead of throwing exceptions when an index/key is missing.

Lists / arrays: use getOrNull

val items = listOf("A", "B", "C")

val first = items.getOrNull(0)   // "A"
val missing = items.getOrNull(10) // null

This is safer than:

val missing = items[10] // Throws IndexOutOfBoundsException

You can combine it with the Elvis operator:

val value = items.getOrNull(10) ?: "Default value"

Lists / arrays: use getOrElse

If you want a fallback value:

val items = listOf("A", "B", "C")

val value = items.getOrElse(10) { index ->
    "No item at index $index"
}

First / last elements safely

Instead of first() or last(), which throw if the collection is empty, use:

val items = emptyList<String>()

val first = items.firstOrNull()
val last = items.lastOrNull()

With a predicate:

val numbers = listOf(1, 2, 3, 4)

val firstEven = numbers.firstOrNull { it % 2 == 0 } // 2
val firstBig = numbers.firstOrNull { it > 10 }      // null

Single element safely

Use singleOrNull() when you expect exactly one matching element:

val users = listOf("Alice", "Bob")

val onlyAlice = users.singleOrNull { it == "Alice" } // "Alice"
val onlyZoe = users.singleOrNull { it == "Zoe" }     // null

Note: singleOrNull() also returns null if there is more than one match.

Maps: safe access by key

Map access already returns nullable values:

val ages = mapOf("Alice" to 30)

val aliceAge = ages["Alice"] // 30
val bobAge = ages["Bob"]     // null

Use a default if needed:

val bobAge = ages["Bob"] ?: 0

Or use getOrDefault:

val bobAge = ages.getOrDefault("Bob", 0)

Check bounds manually if needed

val items = listOf("A", "B", "C")
val index = 2

if (index in items.indices) {
    println(items[index])
}

Summary

Prefer these safe APIs:

list.getOrNull(index)
list.getOrElse(index) { default }
list.firstOrNull()
list.lastOrNull()
list.singleOrNull()
map[key] ?: default
map.getOrDefault(key, default)

Use direct indexing like list[index] only when you are certain the index is valid.

How do I loop through collections using for, foreach and indices in Kotlin?

In Kotlin, you can loop through collections in several common ways depending on whether you need the element, the index, or both.

1. Using for

Use for when you want a simple, readable loop over elements.

val names = listOf("Alice", "Bob", "Charlie")

for (name in names) {
    println(name)
}

Output:

Alice
Bob
Charlie

This works with many Kotlin types, including:

val numbers = arrayOf(1, 2, 3)

for (number in numbers) {
    println(number)
}

2. Using forEach

Use forEach when you prefer a functional style.

val names = listOf("Alice", "Bob", "Charlie")

names.forEach { name ->
    println(name)
}

If the lambda has only one parameter, you can use it:

names.forEach {
    println(it)
}

forEach is useful for concise operations, but a regular for loop is often clearer if you need break, continue, or more complex control flow.


3. Looping with indices

Use indices when you need the index of each element.

val names = listOf("Alice", "Bob", "Charlie")

for (i in names.indices) {
    println("Index $i: ${names[i]}")
}

Output:

Index 0: Alice
Index 1: Bob
Index 2: Charlie

indices gives the valid index range for the collection, such as 0..lastIndex.


4. Using withIndex

If you need both the index and the value, withIndex() is often cleaner than indexing manually.

val names = listOf("Alice", "Bob", "Charlie")

for ((index, name) in names.withIndex()) {
    println("Index $index: $name")
}

5. Using forEachIndexed

The forEach equivalent for index + value is forEachIndexed.

val names = listOf("Alice", "Bob", "Charlie")

names.forEachIndexed { index, name ->
    println("Index $index: $name")
}

Summary

val items = listOf("A", "B", "C")

// Element only
for (item in items) {
    println(item)
}

// Element only, functional style
items.forEach { item ->
    println(item)
}

// Index only / index-based access
for (i in items.indices) {
    println("items[$i] = ${items[i]}")
}

// Index and value
for ((index, item) in items.withIndex()) {
    println("$index -> $item")
}

// Index and value, functional style
items.forEachIndexed { index, item ->
    println("$index -> $item")
}

Use:

  • for (item in items) for simple iteration
  • items.forEach { ... } for concise functional-style iteration
  • items.indices when you need index-based access
  • withIndex() or forEachIndexed when you need both index and value