Create DTOs (POJOs/POCOs)
data class Customer(val name: String, val email: String)
provides a Customer
class with the following functionality:
- getters (and setters in case of
var
s) for all properties equals()
hashCode()
toString()
copy()
component1()
,component2()
, …, for all properties (see Data classes)
Default values for function parameters
fun foo(a: Int = 0, b: String = "") {
... }
Filter a list
val positives = list.filter {
x -> x > 0 }
Or alternatively, even shorter:
val positives = list.filter {
it > 0 }
Learn the difference between Java and Kotlin filtering.
Check the presence of an element in a collection
if ("john@example.com" in emailsList) {
... }
if ("jane@example.com" !in emailsList) {
... }
String interpolation
println("Name $name")
Learn the difference between Java and Kotlin string concatenation.
Instance checks
when (x) {
is Foo -> ...
is Bar -> ...
else -> ...
}
Read-only list
val list = listOf("a", "b", "c")
Read-only map
val map = mapOf("a" to 1, "b" to 2, "c" to 3)
Access a map entry
println(map["key"])