KOTLIN / CATATAN</>↗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 / CATATAN</>↗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() […]
KOTLIN / CATATAN</>↗Kotlin23 Jun 2026
In Kotlin, use isEmpty() / isNotEmpty() to check whether a collection has elements, and use in, contains(), or map-specific methods to check contents. val names = listOf("Alice", "Bob") println(names.isEmpty()) // false println(names.isNotEmpty()) // true println("Alice" in names) // true println("Charlie" !in names) // true For lists and sets: val numbers = setOf(1, 2, 3) if […]
KOTLIN / CATATAN</>↗Kotlin23 Jun 2026
In Kotlin, you can access collection elements safely by using functions that return null instead of throwing exceptions when an index/key is missing. Lists / arrays: use getOrNull val items = listOf("A", "B", "C") val first = items.getOrNull(0) // "A" val missing = items.getOrNull(10) // null This is safer than: val missing = items[10] // […]
KOTLIN / CATATAN</>↗Kotlin23 Jun 2026
In Kotlin, you can loop through collections in several common ways depending on whether you need the element, the index, or both. 1. Using for Use for when you want a simple, readable loop over elements. val names = listOf("Alice", "Bob", "Charlie") for (name in names) { println(name) } Output: Alice Bob Charlie This works […]
KOTLIN / CATATAN</>↗Kotlin23 Jun 2026
In Kotlin, the main collection types are List, Set, and Map. Kotlin provides both read-only and mutable versions: Collection Read-only Mutable List List<T> MutableList<T> Set Set<T> MutableSet<T> Map Map<K, V> MutableMap<K, V> Lists A list is an ordered collection. It can contain duplicate elements. Read-only list val numbers = listOf(1, 2, 3, 3) println(numbers[0]) // […]