Lists in Kotlin
📌 Lists in Kotlin
In Kotlin, a List is a collection that stores a sequence of elements. Lists are ordered and allow duplicate values. Kotlin provides two types of lists:
Immutable List (
List) → Read-only (cannot be modified).Mutable List (
MutableList) → Can be modified (add, remove, or update elements).
✅ 1. Immutable List
Created using
listOf().Cannot be changed once initialized.
📌 Example:
fun () { val fruits = listOf("Apple", "Banana", "Cherry") println(fruits) println("First fruit: ${fruits[0]}") println(Total Fruits: 3
listOf()creates a read-only list.Access elements using the index (
fruits[0]).Use
sizeto get the number of elements.
✅ 2. Mutable List
Created using
mutableListOf().Allows adding, removing, and modifying elements.
📌 Example:
fun () { val numbers = mutableListOf(1, 2, 3) numbers.add(4) // Add element numbers.remove(2) // Remove element numbers[0] = 10 // Update element println(numbers)}
Output:
[10, 3, 4]
add()→ Adds an element.remove()→ Removes a specific element.numbers[0] = 10→ Updates the element at index0.
✅ 3. List Functions in Kotlin
| Function | Description | Example |
|---|---|---|
add(element) | Adds an element to a mutable list | list.add(5) |
remove(element) | Removes an element by value | list.remove("Banana") |
removeAt(index) | Removes an element by index | list.removeAt(1) |
get(index) | Returns element at specified index | list.get(0) |
contains(element) | Checks if list contains a value | list.contains("Apple") |
indexOf(element) | Returns the index of the first match | list.indexOf("Cherry") |
lastIndexOf(element) | Returns the last occurrence index | list.lastIndexOf("Apple") |
isEmpty() | Checks if the list is empty | list.isEmpty() |
size | Returns the number of elements | list.size |
✅ 4. Iterating Through a List
You can iterate through a list using different methods:
📌 Using For Loop:
fun () { val colors = listOf("Red", "Green", "Blue") for (color in colors) { println(color) }}
📌 Using ForEach:
colors.forEach { color -> println(color) }
📌 Using Indices:
for (index in colors.indices) { println("Color at index $index is ${colors[index]}")}
✅ 5. Filtering and Sorting Lists
Kotlin provides powerful functions to manipulate lists:
📌 Filter:
val numbers = listOf(1, 2, 3, 4, 5, 6)val evenNumbers = numbers.filter { it % 2 == 0 }println(evenNumbers) // Output: [2, 4, 6]
📌 Sort:
val names = listOf("John", "Alice", "Bob")val sortedNames = names.sorted()println(sortedNames) // Output: [Alice, Bob, John]
📌 Map:
val nums = listOf(1, 2, 3)val squaredNums = nums.map { it * it }println(squaredNums) // Output: [1, 4, 9]
✅ 6. Combining Lists
You can combine lists using different methods:
📌 Using Plus (+) Operator:
val list1 = listOf(1, 2, 3)val list2 = listOf(4, 5, 6)val combinedList = list1 + list2println(combinedList) // Output: [1, 2, 3, 4, 5, 6]
✅ 7. Conclusion
Use
listOf()for immutable lists.Use
mutableListOf()for mutable lists.Apply functions like
filter(),map(), andsorted()for manipulation.Iterate using loops or
forEach.Combine lists using the
+operator.