https://kotlinlang.org/docs/reference/idioms.html#creating-dtos-pojospocos
data class Customer(val name: String, val email: String) |
https://kotlinlang.org/docs/reference/idioms.html#default-values-for-function-parameters
fun foo(a: Int = 0, b: String = "") { ... } |
https://kotlinlang.org/docs/reference/idioms.html#checking-element-presence-in-a-collection
if ("john@example.com" in emailsList) { ... } if ("jane@example.com" !in emailsList) { ... } |
https://kotlinlang.org/docs/reference/idioms.html#string-interpolation
println("Name $name") |
https://kotlinlang.org/docs/reference/idioms.html#instance-checks
when (x) { is Foo -> ... is Bar -> ... else -> ... } |
Destructuring declaration (https://kotlinlang.org/docs/reference/idioms.html#traversing-a-maplist-of-pairs)
for ((k, v) in map) { println("$k -> $v") } val (a,b) = Pair(1, 2) |
ranges instead of for loops (https://kotlinlang.org/docs/reference/idioms.html#using-ranges):
for (i in 1..100) { ... } // closed range: includes 100 for (i in 1 until 100) { ... } // half-open range: does not include 100 for (x in 2..10 step 2) { ... } for (x in 10 downTo 1) { ... } if (x in 1..10) { ... } |
https://kotlinlang.org/docs/reference/idioms.html#read-only-list
val list = listOf("a", "b", "c") |
https://kotlinlang.org/docs/reference/idioms.html#accessing-a-map
println(map["key"]) map["key"] = value |
https://kotlinlang.org/docs/reference/idioms.html#lazy-property
val p: String by lazy { // compute the string } |
https://kotlinlang.org/docs/reference/idioms.html#extension-functions
fun String.spaceToCamelCase() { ... } "Convert this to camelcase".spaceToCamelCase() |
https://kotlinlang.org/docs/reference/idioms.html#creating-a-singleton
object Resource { val name = "Name" } |
Safe chain and elvis (https://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand)
val files = File("Test").listFiles() println(files?.size ?: "empty") |
https://kotlinlang.org/docs/reference/idioms.html#execute-if-not-null
val value = ... value?.let { ... // execute this block if not null } |
https://kotlinlang.org/docs/reference/idioms.html#return-on-when-statement
fun transform(color: String): Int { return when (color) { "Red" -> 0 "Green" -> 1 "Blue" -> 2 else -> throw IllegalArgumentException("Invalid color param value") } } |
https://kotlinlang.org/docs/reference/idioms.html#trycatch-expression
fun test() { val result = try { count() } catch (e: ArithmeticException) { throw IllegalStateException(e) } // Working with result } |
https://kotlinlang.org/docs/reference/idioms.html#builder-style-usage-of-methods-that-return-unit
fun arrayOfMinusOnes(size: Int): IntArray { return IntArray(size).apply { fill(-1) } } |
https://kotlinlang.org/docs/reference/idioms.html#single-expression-functions
fun theAnswer() = 42 |
https://kotlinlang.org/docs/reference/idioms.html#configuring-properties-of-an-object-apply
val myRectangle = Rectangle().apply { length = 4 breadth = 5 color = 0xFAFAFA } |
Scoped use of objects (https://kotlinlang.org/docs/reference/idioms.html#java-7s-try-with-resources)
val stream = Files.newInputStream(Paths.get("/some/file.txt")) stream.buffered().reader().use { reader -> println(reader.readText()) } |
// public final class Gson { // ... // public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException { // ... inline fun <reified T: Any> Gson.fromJson(json: JsonElement): T = this.fromJson(json, T::class.java) |
Non-local return
fun foo() { listOf(1, 2, 3, 4, 5).forEach { if (it == 3) return // non-local return directly to the caller of foo() print(it) } println("this point is unreachable") } |
.filter.map.reduce (collection processing, higher order functions) (https://kotlinlang.org/docs/reference/idioms.html#filtering-a-list)
val list: List<Int> = listOf(1,2,3,4,5,6) val result = list .filter{it%2 == 0} //select even numbers .map{it*it} // get square of each element .sumByDouble{it.toDouble()} //use one of reduce operations |
Factory method instead of constructors