Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Kotlin list : Arraylist

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).

1. Introduction:

  • 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.

2. Initialization:

// 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")

3. Basic Operations:

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"

4. Iterating Over a List:

for (item in list) {
    println(item)
}

// Using forEach and lambda
list.forEach { item ->
    println(item)
}

5. Common List Functions:

val fruits = listOf("Apple", "Banana", "Cherry", "Date", "Banana")

println(fruits.indexOf("Banana"))     // 1
println(fruits.lastIndexOf("Banana")) // 4
println(fruits.contains("Apple"))     // true

6. List Transformations:

val lengths = fruits.map { it.length }  // [5, 6, 6, 4, 6]

Filtering:

val longNames = fruits.filter { it.length > 5 }  // [Banana, Cherry, Banana]

7. Checking Conditions:

val allLong = fruits.all { it.length > 5 }      // false
val anyLong = fruits.any { it.length > 5 }      // true

8. Conversion:

To convert a MutableList to an immutable List or vice-versa:

val immutable = mutableList.toList()
val newMutableList = list.toMutableList()

9. Sorting:

val sortedList = fruits.sorted()           // Alphabetical order
val sortedByLength = fruits.sortedBy { it.length }

Conclusion:

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.

  1. Creating an ArrayList in Kotlin:

    • ArrayLists can be created using the ArrayList constructor.
    val numbersList = ArrayList<Int>()
    
  2. Adding and removing elements in Kotlin ArrayList:

    • Elements can be added using add and removed using remove methods.
    numbersList.add(42)
    numbersList.remove(42)
    
  3. Accessing elements in a Kotlin ArrayList:

    • Elements can be accessed using index notation.
    val firstElement = numbersList[0]
    
  4. Iterating over an ArrayList in Kotlin:

    • Use various loop constructs or collection functions for iteration.
    for (number in numbersList) {
        // Process each number
    }
    
  5. Sorting an ArrayList in Kotlin:

    • The sort function can be used for sorting.
    numbersList.sort()
    
  6. Filtering and mapping with ArrayList in Kotlin:

    • Utilize functions like filter and map for data transformations.
    val evenNumbers = numbersList.filter { it % 2 == 0 }
    val squaredNumbers = numbersList.map { it * it }
    
  7. Converting Array to ArrayList in Kotlin:

    • Arrays can be converted to ArrayLists using the ArrayList constructor.
    val array = arrayOf(1, 2, 3)
    val arrayList = ArrayList(array.asList())
    
  8. ArrayList functions and methods in Kotlin:

    • ArrayLists offer various functions and methods for common operations.
    val size = numbersList.size
    val lastIndex = numbersList.lastIndex
    
  9. Null safety and ArrayList in Kotlin:

    • ArrayLists can hold nullable types, and null safety principles apply.
    val nullableList: ArrayList<String?> = ArrayList()
    
  10. ArrayList vs MutableList in Kotlin:

    • ArrayList is a specific implementation of MutableList, providing dynamic sizing.
    val mutableList: MutableList<Int> = ArrayList()
    
  11. ArrayList and generic types in Kotlin:

    • ArrayLists can hold elements of a specific type using generics.
    val stringList: ArrayList<String> = ArrayList()