KOTLIN / CATATAN</>↗Kotlin24 Jun 2026
In Kotlin, you can combine multiple collection operations by chaining functions like filter, map, sortedBy, take, groupBy, and others. Each operation returns a new collection, so you can call the next operation directly on the result. val numbers = listOf(1, 2, 3, 4, 5, 6) val result = numbers .filter { it % 2 == […]
KOTLIN / CATATAN</>↗Kotlin24 Jun 2026
In Kotlin, destructuring lets you unpack values from an object into separate variables. It is commonly used with: Pair Map.Entry loops over maps lambda parameters Destructuring a Pair A Pair<A, B> contains two values: first and second. val pair = "Alice" to 25 val (name, age) = pair println(name) // Alice println(age) // 25 This […]
KOTLIN / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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 / CATATAN</>↗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 { […]