KOTLIN / NOTES</>↗Kotlin24 Jun 2026
In Kotlin, associateBy, partition, and zip are collection operations that help transform or split collections. associateBy associateBy creates a Map from a collection by choosing a key for each element. data class User(val id: Int, val name: String) val users = listOf( User(1, "Alice"), User(2, "Bob"), User(3, "Charlie") ) val usersById = users.associateBy { user […]
KOTLIN / NOTES</>↗Kotlin24 Jun 2026
In Kotlin, you can group elements with groupBy, then count how many items are in each group. Basic example val words = listOf("apple", "banana", "apricot", "blueberry", "avocado") val countsByFirstLetter = words .groupBy { it.first() } .mapValues { (_, words) -> words.count() } println(countsByFirstLetter) Output: {a=3, b=2} Here: groupBy { it.first() } groups words by their […]
KOTLIN / NOTES</>↗Kotlin24 Jun 2026
In Kotlin, flatMap is used to transform each element into a collection and then flatten the results into a single list. Basic idea val result = items.flatMap { item -> // return a collection for each item } It is similar to: items.map { … }.flatten() but more concise. Example: flatten nested lists val nested […]
KOTLIN / NOTES</>↗Kotlin24 Jun 2026
In Kotlin collections: map transforms each element into a new value. filter keeps only elements that match a condition. forEach performs an action for each element. map: transform elements Use map when you want to create a new collection by changing each item. val numbers = listOf(1, 2, 3, 4) val doubled = numbers.map { […]
KOTLIN / NOTES</>↗Kotlin23 Jun 2026
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> = […]
KOTLIN / NOTES</>↗Kotlin23 Jun 2026
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() […]