Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
Kotlin provides a rich set of collection classes, and one of the most commonly used is List
. Kotlin's standard library also offers an ArrayList
which is a part of the collections framework. Let's explore Kotlin's List
and its mutable counterpart MutableList
(which is implemented by ArrayList
).
List: Represents an ordered collection of items. It's immutable by default, meaning you can't add or remove items from it after initialization.
MutableList: As the name suggests, it's a mutable version of the List
. You can add, remove, or modify items in a MutableList
.
// Immutable List val list: List<String> = listOf("Apple", "Banana", "Cherry") // Mutable List / ArrayList val mutableList: MutableList<String> = mutableListOf("Apple", "Banana", "Cherry") val arrayList: ArrayList<String> = arrayListOf("Apple", "Banana", "Cherry")
Adding Elements:
mutableList.add("Date") mutableList.add(1, "Mango") // Inserts "Mango" at position 1
Removing Elements:
mutableList.remove("Apple") mutableList.removeAt(0)
Updating Elements:
mutableList[1] = "Pear"
for (item in list) { println(item) } // Using forEach and lambda list.forEach { item -> println(item) }
val fruits = listOf("Apple", "Banana", "Cherry", "Date", "Banana") println(fruits.indexOf("Banana")) // 1 println(fruits.lastIndexOf("Banana")) // 4 println(fruits.contains("Apple")) // true
val lengths = fruits.map { it.length } // [5, 6, 6, 4, 6]
Filtering:
val longNames = fruits.filter { it.length > 5 } // [Banana, Cherry, Banana]
val allLong = fruits.all { it.length > 5 } // false val anyLong = fruits.any { it.length > 5 } // true
To convert a MutableList
to an immutable List
or vice-versa:
val immutable = mutableList.toList() val newMutableList = list.toMutableList()
val sortedList = fruits.sorted() // Alphabetical order val sortedByLength = fruits.sortedBy { it.length }
Kotlin's List
and MutableList
(along with its implementation, ArrayList
) are fundamental data structures that every Kotlin developer should be familiar with. They come equipped with a rich set of functions and capabilities, making them extremely versatile for a wide range of applications.
Creating an ArrayList in Kotlin:
ArrayList
constructor.val numbersList = ArrayList<Int>()
Adding and removing elements in Kotlin ArrayList:
add
and removed using remove
methods.numbersList.add(42) numbersList.remove(42)
Accessing elements in a Kotlin ArrayList:
val firstElement = numbersList[0]
Iterating over an ArrayList in Kotlin:
for (number in numbersList) { // Process each number }
Sorting an ArrayList in Kotlin:
sort
function can be used for sorting.numbersList.sort()
Filtering and mapping with ArrayList in Kotlin:
filter
and map
for data transformations.val evenNumbers = numbersList.filter { it % 2 == 0 } val squaredNumbers = numbersList.map { it * it }
Converting Array to ArrayList in Kotlin:
ArrayList
constructor.val array = arrayOf(1, 2, 3) val arrayList = ArrayList(array.asList())
ArrayList functions and methods in Kotlin:
val size = numbersList.size val lastIndex = numbersList.lastIndex
Null safety and ArrayList in Kotlin:
val nullableList: ArrayList<String?> = ArrayList()
ArrayList vs MutableList in Kotlin:
val mutableList: MutableList<Int> = ArrayList()
ArrayList and generic types in Kotlin:
val stringList: ArrayList<String> = ArrayList()