习语集合
Kotlin 中常用的随机习语集合。如果你有喜欢的习语,可以通过提交 pull request 来贡献它。
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
vars) 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.
Read standard input safely
// Reads a string and returns null if the input can't be converted into an integer. For example: Hi there!
val wrongInt = readln().toIntOrNull()
println(wrongInt)
// null
// Reads a string that can be converted into an integer and returns an integer. For example: 13
val correctInt = readln().toIntOrNull()
println(correctInt)
// 13
For more information, see Read standard input.
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"])
map["key"] = valueTraverse a map or a list of pairs
for ((k, v) in map) {
println("$k -> $v")
}
k and v can be any convenient names, such as name and age.
Iterate over a range
for (i in 1..100) { ... } // closed-ended range: includes 100
for (i in 1..<100) { ... } // open-ended range: does not include 100
for (x in 2..10 step 2) { ... }
for (x in 10 downTo 1) { ... }
(1..10).forEach { ... }Lazy property
val p: String by lazy { // the value is computed only on first access
// compute the string
}Extension functions
fun String.spaceToCamelCase() { ... }
"Convert this to camelcase".spaceToCamelCase()Create a singleton
object Resource {
val name = "Name"
}Use inline value classes for type-safe values
@JvmInline
value class EmployeeId(private val id: String)
@JvmInline
value class CustomerId(private val id: String)
If you accidentally mix up EmployeeId and CustomerId, a compilation error is triggered.爱吃喝
The
@JvmInlineannotation is only needed for JVM backends.
Instantiate an abstract class
abstract class MyAbstractClass {
abstract fun doSomething()
abstract fun sleep()
}
fun main() {
val myObject = object : MyAbstractClass() {
override fun doSomething() {
// ...
}
override fun sleep() { // ...
}
}
myObject.doSomething()
}If-not-null shorthand
val files = File("Test").listFiles()
println(files?.size) // size is printed if files is not nullIf-not-null-else shorthand
val files = File("Test").listFiles()
// For simple fallback values:
println(files?.size ?: "empty") // if files is null, this prints "empty"
// To calculate a more complicated fallback value in a code block, use `run`
val filesSize = files?.size ?: run {
val someSize = getSomeSize()
someSize * 2
}
println(filesSize)Execute an expression if null
val values = ...
val email = values["email"] ?: throw IllegalStateException("Email is missing!")爱河池Get first item of a possibly empty collection
val emails = ... // might be empty
val mainEmail = emails.firstOrNull() ?: ""
Learn the difference between Java and Kotlin first item getting.
Execute if not null
val value = ...
value?.let {
... // execute this block if not null
}Map nullable value if not null
val value = ...
val mapped = value?.let { transformValue(it) } ?: defaultValue
// defaultValue is returned if the value or the transform result is null.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")
}
}try-catch expression
fun test() {
val result = try {
count()
} catch (e: ArithmeticException) {
throw IllegalStateException(e)
}
// Working with result
}if expression
val y = if (x == 1) {
"one"
} else if (x == 2) {
"two"
} else {
"other"
}Builder-style usage of methods that return Unit
fun arrayOfMinusOnes(size: Int): IntArray {
return IntArray(size).apply { fill(-1) }
}Single-expression functions
fun theAnswer() = 42
This is equivalent to
fun theAnswer(): Int {
return 42
}
This can be effectively combined with other idioms, leading to shorter code. For example, with the when expression:
fun transform(color: String): Int = when (color) {
"Red" -> 0
"Green" -> 1
"Blue" -> 2
else -> throw IllegalArgumentException("Invalid color param value")
}Call multiple methods on an object instance (with)
class Turtle {
fun penDown()
fun penUp()
fun turn(degrees: Double)
fun forward(pixels: Double)
}
val myTurtle = Turtle()
with(myTurtle) { //draw a 100 pix square
penDown()
for (i in 1..4) {
forward(100.0)
turn(90.0)
}
penUp()
}Configure properties of an object (apply)
val myRectangle = Rectangle().apply {
length = 4
breadth = 5
color = 0xFAFAFA
}
This is useful for configuring properties that aren't present in the object constructor.
Java 7's try-with-resources
val stream = Files.newInputStream(Paths.get("/some/file.txt"))
stream.buffered().reader().use { reader ->
println(reader.readText())
}Generic function that requires the generic type information
// 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)Swap two variables
var a = 1
var b = 2
a = b.also { b = a }
.also的执行顺序是这样的:
首先,计算 b.also { ... }这个表达式的结果。
为了得到结果,需要执行 also里面的代码块 { b = a }。
此时 a = 1,b = 2。
执行 b = a→ b变成 1。
also返回的是调用它的对象,也就是原来的 b(值为 2)。
然后,将 also返回的值(2)赋值给 a。
a = 2
所以最终:
a = 2
b = 1
你可能会觉得“先执行了 b = a,那 a不应该还是 1吗?”
——注意,also里的 b = a改的是 b,
而 also返回的是改变之前的 b的值(2),这个 2最后才赋给 a。
简单总结:b.also { b = a }相当于:
把 b的旧值(2)存起来。
执行 b = a(此时 b变为 1)。
返回之前存起来的旧值(2)给 a。
所以结果是 a=2, b=1,成功交换了值。
注意 .let的区别
先计算 b.let { b = a }:
进入 lambda 时,a = 1,b = 2
执行 b = a→ b变成 1
let返回 lambda 的最后一行结果,也就是 b = a这个赋值表达式的值
——在 Kotlin 中,赋值表达式返回的是 Unit(或者说没有有意义的值)
所以这里有个陷阱:b.let { b = a }返回的是 Unit(即 kotlin.Unit),而不是数字。
因此:
a = Unit(不再是数字)
b = 1
这和 also版本完全不同——用 let无法实现交换,反而会把 a的类型搞乱。
Mark code as incomplete (TODO)
Kotlin's standard library has a TODO() function that will always throw a NotImplementedError. Its return type is Nothing so it can be used regardless of expected type. There's also an overload that accepts a reason parameter:
fun calcTaxes(): BigDecimal = TODO("Waiting for feedback from accounting")
IntelliJ IDEA's kotlin plugin understands the semantics of TODO() and automatically adds a code pointer in the TODO tool window.





