https://github.com/benct/kotlin-cheat-sheet
:star: Kotlin <3 Cheat Sheet, Collection Extension Functions and General Examples
https://github.com/benct/kotlin-cheat-sheet
aggregators cheatsheet collections examples kotlin kotlin-extensions kotlin-language lamdba transformers
Last synced: 11 months ago
JSON representation
:star: Kotlin <3 Cheat Sheet, Collection Extension Functions and General Examples
- Host: GitHub
- URL: https://github.com/benct/kotlin-cheat-sheet
- Owner: benct
- License: mit
- Created: 2020-02-06T22:01:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-27T11:59:54.000Z (over 6 years ago)
- Last Synced: 2025-03-22T22:43:30.983Z (over 1 year ago)
- Topics: aggregators, cheatsheet, collections, examples, kotlin, kotlin-extensions, kotlin-language, lamdba, transformers
- Homepage:
- Size: 9.77 KB
- Stars: 7
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Kotlin Cheat Sheet
[](https://www.repostatus.org/#wip)
## Table of Contents
1. [Collections](#collections)
1. [Arrays](#arrays)
1. [Lists](#lists)
1. [Sets](#sets)
1. [Maps](#maps)
1. [Mutability](#mutability)
1. [Transformers](#transformers)
1. [Associate](#associate)
1. [Map](#map)
1. [MapKeys](#mapkeys)
1. [MapValues](#mapvalues)
1. [Sorting](#sorting)
1. [Flattening](#flattening)
1. [Aggregators](#aggregators)
1. [Fold](#fold)
1. [Reduce](#reduce)
1. [Grouping](#grouping)
1. [Chunked](#chunked)
## Collections
#### Arrays
```kotlin
val intArray: Array = arrayOf(1, 2, 3)
val primitiveIntArray: IntArray = intArrayOf(1, 2, 3)
val primitiveDoubleArray: DoubleArray = doubleArrayOf(1, 2, 3)
val primitiveLongArray: LongArray = longArrayOf(1, 2, 3)
val primitiveFloatArray: FloatArray = floatArrayOf(1, 2, 3)
val copyOfArray: Array = arrayOf(1, 2, 3).copyOf()
val partialCopyOfArray: Array = arrayOf(1, 2, 3).copyOfRange(0, 2)
```
#### Lists
```kotlin
val intList: List = listOf(1, 2, 3)
val arrayList: List = arrayListOf(1, 2, 3)
val emptyList: List = emptyList()
val listOfNotNull: List = listOfNotNull(1, null, 3)
```
#### Sets
```kotlin
val set: Set = setOf(1, 2, 3)
val hashSet: Set = hashSetOf(1, 2, 3)
val linkedSet: Set = linkedSetOf(1, 2, 3)
val emptySet: Set = emptySet()
```
#### Maps
```kotlin
val map: Map = mapOf("foo" to 1, "bar" to 2)
val hashMap: Map = hashMapOf("foo" to 1, "bar" to 2)
val linkedMap: Map = linkedMapOf("foo" to 1, "bar" to 2)
val emptyMap: Map = emptyMap()
```
#### Mutability
```kotlin
val mutableList: MutableList = mutableListOf(1, 2, 3)
val mutableSet: MutableSet = mutableSetOf(1)
var mutableMap: MutableMap = mutableMapOf("foo" to 1, "bar" to 2)
```
## Transformers
```kotlin
listOf(1, 2, 3).reversed() // [3, 2, 1]
listOf(1, 2, 3).partition { it > 2 } // Pair([3], [1,2])
listOf(1, 2, 3).slice(1..2) // [2, 3]
```
#### Associate
Returns a map containing key-value pairs created by lambda.
```kotlin
listOf(1, 2, 3).associate { "key$it" to "val$it" } // {key1=val1, key2=val2, key3=val3}
listOf(1, 2, 3).associateWith { "val$it" } // {1=val1, 2=val2, 3=val3}
listOf(1, 2, 3).associateBy { "key$it" } // {key1=1, key2=2, key3=3}
```
#### Map
Returns a new collection by transforming all elements from the initial collection.
```kotlin
listOf(1, 2, 3).map { it + 1 } // [2, 3, 4]
listOf(1, 2, 3).mapIndexed { idx, value -> if (idx == 0) value else value + 1 } // [1, 3, 4]
listOf(1, 2, 3).mapNotNull { if (it == 1) null else it + 1 } // [3, 4]
listOf(1, 2, 3).mapIndexedNotNull { idx, value -> if (idx == 0) null else value + 1 } // [3, 4]
```
#### MapKeys
Transforms all keys from a map, where lamdba return value is the new key of original value.
```kotlin
mapOf("foo" to 1, "bar" to 2).mapKeys { "${it.key}key" } // {fookey=1, barkey=2}
```
#### MapValues
Transforms all values from a map, where lamdba return value is the new value for the original key.
```kotlin
mapOf("foo" to 1, "bar" to 2).mapValues { it.value + 1 } // {foo=2, bar=3}
```
#### Sorting
Sorts collection ascending (default) or descending, based on what lambda returns.
```kotlin
listOf(2, 1, 3).sorted() // [1, 2, 3]
listOf(2, 1, 3).sorted { it } // [1, 2, 3]
listOf(2, 1, 3).sortedByDescending() // [3, 2, 1]
listOf(2, 1, 3).sortedWith(Comparator { x, y -> x - y }) // [1, 2, 3]
```
#### Flattening
Collects (and transforms) elements of all passed collections.
```kotlin
listOf(listOf(1, 2), listOf(3)).flatten() // [1, 2, 3]
listOf(listOf(1, 2), listOf(3)).flatMap { it } // [1, 2, 3]
listOf(listOf(1, 2), listOf(3)).flatMap { iterable -> iterable.map { it + 1 } } // [2, 3, 4]
```
## Aggregators
```kotlin
listOf(1, 2, 3).count() // 3
listOf(1, 2, 3).count { it == 3 } // 1
listOf(1, 2, 3).average() // 2.0
listOf(1, 2, 3).max() // 3
listOf(1, 2, 3).maxBy { it * 5 } // 3
listOf(1, 2, 3).min() // 1
listOf(1, 2, 3).minBy { it * 5 } // 1
listOf(1, 2, 3).sum() // 6
listOf(1, 2, 3).sumBy { if (it == 3) 6 else it } // 9 (1+2+6)
listOf(1, 2, 3).sumByDouble { it.toDouble() + 1.0 } // 9 (2+3+4)
```
#### Fold
Accumulates values starting with initial supplied value and applying operation from left to right (default) or right to left (foldRight).
```kotlin
listOf(1, 2, 3).fold(5) { accumulator, value -> accumulator + value } // 11 (5+1+2+3)
listOf(1, 2, 3).foldIndexed(5) { idx, accumulator, value ->
if (idx == 0) accumulator else accumulator + value // 10 (5+2+3)
}
listOf(1, 2, 3).foldRight(5) { value, accumulator -> accumulator + value } // 11 (5+3+2+1)
listOf(1, 2, 3).foldRightIndexed(5) { idx, value, accumulator ->
if (idx == 0) accumulator else accumulator + value // 8 (5+2+1)
}
```
#### Reduce
Accumulates values starting with first value and applying operation from left to right (default) or right to left (foldRight).
```kotlin
listOf(1, 2, 3).reduce { accumulator, value -> accumulator + value } // 6 (1+2+3)
listOf(1, 2, 3).reduceIndexed { idx, accumulator, value ->
if (idx == 0) accumulator else accumulator + value // 5 (2+3)
}
listOf(1, 2, 3).reduceRight { value, accumulator -> accumulator + value } // 6 (3+2+1)
listOf(1, 2, 3).reduceRightIndexed { idx, value, accumulator ->
if (idx == 0) accumulator else accumulator + value // 5 (3+2)
}
```
#### Grouping
Uses value returned from (first) lambda to group elements of the collection.
```kotlin
listOf(1, 2, 3).groupBy { "key" } // {key=[1, 2, 3]}
listOf(1, 2, 3).groupBy({ "key$it" }) { it + 1 } // {key1=[2], key2=[3], key3=[4]}
listOf(1, 2, 3).groupByTo(mutableMapOf(), { it }) { it + 10 } // {1=[11], 2=[12], 3=[13]}
```
#### Chunked
Splits this collection into a list of lists, each not exceeding the given size.
```kotlin
listOf("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
.chunked(3) // [[one, two, three], [four, five, six], [seven, eight, nine], [ten]]
```