0% found this document useful (0 votes)
202 views

Kotlin Cheat Sheet

This document provides an overview of Kotlin basics including: 1) Declaring functions and variables, control structures like if/else and for loops, and functions types. 2) Classes and objects including properties, inheritance, and data classes. 3) Collection literals for creating lists, sets, maps and sequences and common collection processing functions like filter, map, and reduce. 4) Extension functions, delegates like lazy, and overview of additional topics like functions.

Uploaded by

Star
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views

Kotlin Cheat Sheet

This document provides an overview of Kotlin basics including: 1) Declaring functions and variables, control structures like if/else and for loops, and functions types. 2) Classes and objects including properties, inheritance, and data classes. 3) Collection literals for creating lists, sets, maps and sequences and common collection processing functions like filter, map, and reduce. 4) Extension functions, delegates like lazy, and overview of additional topics like functions.

Uploaded by

Star
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

BASICS CLASSES

“Hello, World” program Primary constructor


fun main(args: Array<String>) { val declares a read-only property, var a mutable one
println("Hello, World") class Person(val name: String, var age: Int)
} // name is read-only, age is mutable
Declaring function Inheritance
fun sum(a: Int, b: Int): Int { open class Person(val name: String) {
return a + b open fun hello() = "Hello, I am $name"
} // Final by default so we need open
}
Single-expression function
class PolishPerson(name: String) : Person(name) {
fun sum(a: Int, b: Int) = a + b override fun hello() = $name"
Declaring variables }
val name = ”Marcin” // Can't be changed Properties with assessors
var age = 5 // Can be changed class Person(var name: String, var surname: String) {
age++ var fullName: String
Variables with nullable types get() = "$name $surname"
set(value) {
var name: String? = null
val (first, rest) = value.split(" ", limit = 2)
val length: Int
name = first
length = name?.length ?: 0
surname = rest
// length, or 0 if name is null
}
length = name?.length ?: return
}
// length, or return when name is null
length = name?.length ?: throw Error() Data classes
// length, or throw error when name is null data class Person(val name: String, var age: Int)
val mike = Person("Mike", 23)
CONTROL STRUCTURES
Modifier data adds:
If as an expression 1. toString that displays all primary constructor
fun bigger(a: Int, b: Int) = if (a > b) a else b properties
print(mike.toString()) // Person(name=Mike, age=23)
For loop
val list = listOf("A", "B", "C") 2. equals that compares all primary constructor
for (element in list) { properties
println(element) print(mike == Person("Mike", 23)) // True
} print(mike == Person("Mike", 21)) // False
When expression
3. hashCode that is based on all primary
fun numberTypeName(x: Number) = when(x) {
constructor properties
0 -> "Zero" // Equality check
val hash = mike.hashCode()
in 1..4 -> "Four or less" // Range check
print(hash == Person("Mike", 23).hashCode()) // True
5, 6, 7 -> "Five to seven" // Multiple values
print(hash == Person("Mike", 21).hashCode()) // False
is Byte -> "Byte" // Type check
else -> "Some number"
4. component1, component2 etc. that allows
}
deconstruction
When expression with predicates val (name, age) = mike
fun signAsString(x: Int)= when { print("$name $age") // Mike 23
x < 0 -> "Negative"
x == 0 -> "Zero" 5. copy that returns copy of object with concrete
else -> "Positive" properties changed
} val jake = mike.copy(name = "Jake")

Learn Kotlin with us: www.kt.academy


COLLECTION LITERALS partition - splits into pair of lists
val (even, odd) = l.partition { it % 2 == 0 }
listOf(1,2,3,4) // List<Int> print(even) // [2, 4]
mutableListOf(1,2,3,4) // MutableList<Int> print(odd) // [1, 3]
min/max/minBy/maxBy
setOf("A", "B", "C") // Set<String> l.min() // 1, possible because we can compare Int
mutableSetOf("A", "B", "C") // MutableSet<String> l.minBy { -it } // 4
l.max() // 4, possible because we can compare Int
arrayOf('a', 'b', 'c') // Array<Char> l.maxBy { -it } // 1
first/firstBy
mapOf(1 to "A", 2 to "B") // Map<Int, String> l.first() // 1
mutableMapOf(1 to "A", 2 to "B") l.first { it % 2 == 0 } // 2 (first even number)
// MutableMap<Int, String> count - count elements matched by predicate
l.count { it % 2 == 0 } // 2
sequenceOf(4,3,2,1) // Sequence<Int> sorted/sortedBy - returns sorted collection
listOf(2,3,1,4).sorted() // [1, 2, 3, 4]
1 to "A" // Pair<Int, String> l.sortedBy { it % 2 } // [2, 4, 1, 3]
groupBy - group elements on collection by key
List(4) { it * 2 } // List<Int> l.groupBy { it % 2 } // Map: {1=[1, 3], 0=[2, 4]}
generateSequence(4) { it + 2 } // Sequence<Int> distinct/distinctBy - returns only unique elements
listOf(1,1,2,2).distinct() // [1, 2]
COLLECTION PROCESSING
Mutable vs immutable
students
collection processing functions
.filter { it.passing && it.averageGrade > 4.0 }
// Only passing students val list = mutableListOf(3,4,2,1)
.sortedByDescending { it.averageGrade } val sortedResult = list.sorted() // Returns sorted
// Starting from ones with biggest grades println(sortedResult) // [1, 2, 3, 4]
.take(10) // Take first 10 println(list) // [3, 4, 2, 1]
.sortedWith(compareBy({ it.surname }, { it.name })) val sortResult = list.sort() // Sorts mutable collection
// Sort by surname and then name println(sortResult) // kotlin.Unit
println(list) // [1, 2, 3, 4]
generateSequence(0) { it + 1 }
// Infinitive sequence of next numbers starting on 0
.filter { it % 2 == 0 } // Keep only even
EXTENSION FUNCTIONS
.map { it * 3 } // Triple every one
TO ANY OBJECT
.take(100) // Take first 100
.average() // Count average Returns
Results of
Receiver
lambda
Most important functions for collection processing Reference
val l = listOf(1,2,3,4) to receiver
filter - returns only elements matched by predicate
l.filter { it % 2 == 0 } // [2, 4] it also let
map - returns elements after transformation
this apply run/with
l.map { it * 2 } // [2, 4, 6, 8]
flatMap - returns elements yielded from results of trans.
l.flatMap { listOf(it, it + 10) } // [1, 11, 2, 12, 3, 13, 4, 14] val dialog = Dialog().apply {
fold/reduce - accumulates elements title = "Dialog title"
l.fold(0.0) { acc, i -> acc + i } // 10.0 onClick { print("Clicked") }
l.reduce { acc, i -> acc * i } // 24 }
forEach/onEach - perfo s an action on every element
l.forEach { print(it) } // Prints 1234, returns Unit
l.onEach { print(it) } // Prints 1234, returns [1, 2, 3, 4]

Learn Kotlin with us: www.kt.academy


FUNCTIONS DELEGATES
Function types Lazy - calculates value before first usage
val i by lazy { print("init "); 10 }
()->Unit - takes no arguments and returns nothing (Unit).
print(i) // Prints: init 10
(Int, Int)->Int - takes two arguments of type Int
print(i) // Prints: 10
and returns Int.
(()->Unit)->Int - takes another function
notNull - returns last setted value, or throws error
and returns Int.
if no value has been set
(Int)->()->Unit - takes argument of type Int
and returns function.
observable/vetoable - calls function every time
Function literals value changes. In vetoable function also decides
val add: (Int, Int) -> Int = { i, j -> i + j } if new value should be set.
// Simple lambda expression var name by observable("Unset") { p, old, new ->
println("${p.name} changed $old -> $new")
val printAndDouble: (Int) -> Int = { }
println(it) name = "Marcin"
// When single parameter, we can reference it using `it` // Prints: name changed Unset -> Marcin
it * 2 // In lambda, last expression is returned
} Map/MutableMap - finds value on map by property
name
// Anonymous function alternative val map = mapOf("a" to 10)
val printAndDoubleFun: (Int) -> Int = fun(i: Int): Int { val a by map
println(i) print(a) // Prints: 10
return i * 2 // Needs return like any function
} VISIBILITY MODIFIERS
val i = printAndDouble(10) // 10 Modifier Class members Top-level
print(i) // 20 Visible Visible
Public (default)
everywhere everywhere
Extension functions Visible only in Visible in
Private
fun Int.isEven() = this % 2 == 0 the same class the same file
print(2.isEven()) // true Visible only in
Protected the same class Not allowed
fun List<Int>.average() = 1.0 * sum() / size and subclasses
print(listOf(1, 2, 3, 4).average()) // 2.5
Visible in the Visible in the
Internal same module if same module
class is accessible

VARIANCE MODIFIERS
Invariance Covariance Contravariance
class Box <T> class Box <out T> class Box <in T>

Number Box<Number> Box<Number> Box<Number>

Int Box<Int> Box<Int> Box<Int>

Learn Kotlin with us: www.kt.academy

You might also like