KOTLIN / NOTES</>↗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 / NOTES</>↗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 / NOTES</>↗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 / NOTES</>↗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]) // […]
KOTLIN / NOTES</>↗Kotlin21 Jun 2026
In Kotlin, the idiomatic way to implement the Singleton pattern is to use an object declaration. object AppConfig { val appName = "MyApp" fun printConfig() { println("App name: $appName") } } You use it directly by its name: fun main() { AppConfig.printConfig() println(AppConfig.appName) } Kotlin guarantees that an object declaration: has exactly one instance is […]
KOTLIN / NOTES</>↗Kotlin21 Jun 2026
You can mix OOP and functional programming in Kotlin by using objects/classes to model state, identity, boundaries, and domain concepts, while using functions/lambdas to model behavior, transformation, policies, and workflows. Kotlin is especially good at this because it supports: Classes, interfaces, inheritance, encapsulation Data classes and sealed classes Lambdas and higher-order functions Immutability with val […]