{"id":21970586,"url":"https://github.com/benct/kotlin-cheat-sheet","last_synced_at":"2025-08-11T07:18:01.859Z","repository":{"id":98422863,"uuid":"238793777","full_name":"benct/kotlin-cheat-sheet","owner":"benct","description":":star: Kotlin \u003c3 Cheat Sheet, Collection Extension Functions and General Examples","archived":false,"fork":false,"pushed_at":"2020-02-27T11:59:54.000Z","size":10,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T22:43:30.983Z","etag":null,"topics":["aggregators","cheatsheet","collections","examples","kotlin","kotlin-extensions","kotlin-language","lamdba","transformers"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benct.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-06T22:01:28.000Z","updated_at":"2024-10-06T04:33:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"bfe5621f-5427-47ba-87a6-d473c6f89b07","html_url":"https://github.com/benct/kotlin-cheat-sheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/benct/kotlin-cheat-sheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benct%2Fkotlin-cheat-sheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benct%2Fkotlin-cheat-sheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benct%2Fkotlin-cheat-sheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benct%2Fkotlin-cheat-sheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benct","download_url":"https://codeload.github.com/benct/kotlin-cheat-sheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benct%2Fkotlin-cheat-sheet/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269844415,"owners_count":24484193,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aggregators","cheatsheet","collections","examples","kotlin","kotlin-extensions","kotlin-language","lamdba","transformers"],"created_at":"2024-11-29T14:40:40.882Z","updated_at":"2025-08-11T07:18:01.817Z","avatar_url":"https://github.com/benct.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Kotlin Cheat Sheet\n\n[![Project Status: WIP](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)\n\n\n## Table of Contents\n\n1. [Collections](#collections)\n    1. [Arrays](#arrays)\n    1. [Lists](#lists)\n    1. [Sets](#sets)\n    1. [Maps](#maps)\n    1. [Mutability](#mutability)\n1. [Transformers](#transformers)\n    1. [Associate](#associate)\n    1. [Map](#map)\n    1. [MapKeys](#mapkeys)\n    1. [MapValues](#mapvalues)\n    1. [Sorting](#sorting)\n    1. [Flattening](#flattening)\n1. [Aggregators](#aggregators)\n    1. [Fold](#fold)\n    1. [Reduce](#reduce)\n    1. [Grouping](#grouping)\n    1. [Chunked](#chunked)\n\n\n## Collections\n\n#### Arrays\n```kotlin\nval intArray: Array\u003cInt\u003e = arrayOf(1, 2, 3)\nval primitiveIntArray: IntArray = intArrayOf(1, 2, 3)\nval primitiveDoubleArray: DoubleArray = doubleArrayOf(1, 2, 3)\nval primitiveLongArray: LongArray = longArrayOf(1, 2, 3)\nval primitiveFloatArray: FloatArray = floatArrayOf(1, 2, 3)\n\nval copyOfArray: Array\u003cInt\u003e = arrayOf(1, 2, 3).copyOf()\nval partialCopyOfArray: Array\u003cInt\u003e = arrayOf(1, 2, 3).copyOfRange(0, 2)\n```\n\n#### Lists\n```kotlin\nval intList: List\u003cInt\u003e = listOf(1, 2, 3)\nval arrayList: List\u003cInt\u003e = arrayListOf(1, 2, 3)\nval emptyList: List\u003cInt\u003e = emptyList()\n\nval listOfNotNull: List\u003cInt\u003e = listOfNotNull(1, null, 3)\n```\n\n#### Sets\n```kotlin\nval set: Set\u003cInt\u003e = setOf(1, 2, 3)\nval hashSet: Set\u003cInt\u003e = hashSetOf(1, 2, 3)\nval linkedSet: Set\u003cInt\u003e = linkedSetOf(1, 2, 3)\n\nval emptySet: Set\u003cInt\u003e = emptySet()\n```\n\n#### Maps\n```kotlin\nval map: Map\u003cString, Int\u003e = mapOf(\"foo\" to 1, \"bar\" to 2)\nval hashMap: Map\u003cString, Int\u003e = hashMapOf(\"foo\" to 1, \"bar\" to 2)\nval linkedMap: Map\u003cString, Int\u003e = linkedMapOf(\"foo\" to 1, \"bar\" to 2)\n\nval emptyMap: Map\u003cString, Int\u003e = emptyMap()\n```\n\n#### Mutability\n```kotlin\nval mutableList: MutableList\u003cInt\u003e = mutableListOf(1, 2, 3)\nval mutableSet: MutableSet\u003cInt\u003e = mutableSetOf(1)\nvar mutableMap: MutableMap\u003cString, Int\u003e = mutableMapOf(\"foo\" to 1, \"bar\" to 2)\n```\n\n## Transformers\n\n```kotlin\nlistOf(1, 2, 3).reversed()   // [3, 2, 1]\nlistOf(1, 2, 3).partition { it \u003e 2 }   // Pair([3], [1,2])\nlistOf(1, 2, 3).slice(1..2)   // [2, 3]\n```\n\n#### Associate\nReturns a map containing key-value pairs created by lambda.\n```kotlin\nlistOf(1, 2, 3).associate { \"key$it\" to \"val$it\" }   // {key1=val1, key2=val2, key3=val3}\nlistOf(1, 2, 3).associateWith { \"val$it\" }   // {1=val1, 2=val2, 3=val3}\nlistOf(1, 2, 3).associateBy { \"key$it\" }   // {key1=1, key2=2, key3=3}\n```\n\n#### Map\nReturns a new collection by transforming all elements from the initial collection.\n```kotlin\nlistOf(1, 2, 3).map { it + 1 }   // [2, 3, 4]\nlistOf(1, 2, 3).mapIndexed { idx, value -\u003e if (idx == 0) value else value + 1 }   // [1, 3, 4]\nlistOf(1, 2, 3).mapNotNull { if (it == 1) null else it + 1 }   // [3, 4]\nlistOf(1, 2, 3).mapIndexedNotNull { idx, value -\u003e if (idx == 0) null else value + 1 }   // [3, 4]\n```\n\n#### MapKeys\nTransforms all keys from a map, where lamdba return value is the new key of original value.\n```kotlin\nmapOf(\"foo\" to 1, \"bar\" to 2).mapKeys { \"${it.key}key\" }   // {fookey=1, barkey=2}\n```\n\n#### MapValues\nTransforms all values from a map, where lamdba return value is the new value for the original key.\n```kotlin\nmapOf(\"foo\" to 1, \"bar\" to 2).mapValues { it.value + 1 }   // {foo=2, bar=3}\n```\n\n#### Sorting\nSorts collection ascending (default) or descending, based on what lambda returns.\n```kotlin\nlistOf(2, 1, 3).sorted()   // [1, 2, 3]\nlistOf(2, 1, 3).sorted { it }   // [1, 2, 3]\nlistOf(2, 1, 3).sortedByDescending()   // [3, 2, 1]\nlistOf(2, 1, 3).sortedWith(Comparator { x, y -\u003e x - y })   // [1, 2, 3]\n```\n\n#### Flattening\nCollects (and transforms) elements of all passed collections.\n```kotlin\nlistOf(listOf(1, 2), listOf(3)).flatten()   // [1, 2, 3]\nlistOf(listOf(1, 2), listOf(3)).flatMap { it }   // [1, 2, 3]\nlistOf(listOf(1, 2), listOf(3)).flatMap { iterable -\u003e iterable.map { it + 1 } }   // [2, 3, 4]\n```\n\n## Aggregators\n\n```kotlin\nlistOf(1, 2, 3).count()   // 3\nlistOf(1, 2, 3).count { it == 3 }  // 1\n\nlistOf(1, 2, 3).average()   // 2.0\n\nlistOf(1, 2, 3).max()   // 3\nlistOf(1, 2, 3).maxBy { it * 5 }   // 3\n\nlistOf(1, 2, 3).min()   // 1\nlistOf(1, 2, 3).minBy { it * 5 }   // 1\n\nlistOf(1, 2, 3).sum()   // 6\nlistOf(1, 2, 3).sumBy { if (it == 3) 6 else it }   // 9 (1+2+6)\nlistOf(1, 2, 3).sumByDouble { it.toDouble() + 1.0 }   // 9 (2+3+4)\n```\n\n#### Fold\nAccumulates values starting with initial supplied value and applying operation from left to right (default) or right to left (foldRight).\n```kotlin\nlistOf(1, 2, 3).fold(5) { accumulator, value -\u003e accumulator + value }   // 11 (5+1+2+3)\nlistOf(1, 2, 3).foldIndexed(5) { idx, accumulator, value -\u003e\n   if (idx == 0) accumulator else accumulator + value   // 10 (5+2+3)\n}\n\nlistOf(1, 2, 3).foldRight(5) { value, accumulator -\u003e accumulator + value }   // 11 (5+3+2+1)\nlistOf(1, 2, 3).foldRightIndexed(5) { idx, value, accumulator -\u003e\n   if (idx == 0) accumulator else accumulator + value   // 8 (5+2+1)\n}\n```\n\n#### Reduce\nAccumulates values starting with first value and applying operation from left to right (default) or right to left (foldRight).\n```kotlin\nlistOf(1, 2, 3).reduce { accumulator, value -\u003e accumulator + value }   // 6 (1+2+3)\nlistOf(1, 2, 3).reduceIndexed { idx, accumulator, value -\u003e\n   if (idx == 0) accumulator else accumulator + value   // 5 (2+3)\n}\n\nlistOf(1, 2, 3).reduceRight { value, accumulator -\u003e accumulator + value }   // 6 (3+2+1)\nlistOf(1, 2, 3).reduceRightIndexed { idx, value, accumulator -\u003e\n   if (idx == 0) accumulator else accumulator + value   // 5 (3+2)\n}\n```\n\n#### Grouping\nUses value returned from (first) lambda to group elements of the collection.\n```kotlin\nlistOf(1, 2, 3).groupBy { \"key\" }   // {key=[1, 2, 3]}\nlistOf(1, 2, 3).groupBy({ \"key$it\" }) { it + 1 }   // {key1=[2], key2=[3], key3=[4]}\n\nlistOf(1, 2, 3).groupByTo(mutableMapOf(), { it }) { it + 10 }   // {1=[11], 2=[12], 3=[13]}\n```\n\n#### Chunked\nSplits this collection into a list of lists, each not exceeding the given size.\n```kotlin\nlistOf(\"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\", \"ten\")\n    .chunked(3)   // [[one, two, three], [four, five, six], [seven, eight, nine], [ten]]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenct%2Fkotlin-cheat-sheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenct%2Fkotlin-cheat-sheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenct%2Fkotlin-cheat-sheet/lists"}