Skip to content

Instantly share code, notes, and snippets.

@meidikawardana
Last active December 28, 2020 03:49
Show Gist options
  • Save meidikawardana/087b1904103ab63719041af7b6e46d04 to your computer and use it in GitHub Desktop.
Save meidikawardana/087b1904103ab63719041af7b6e46d04 to your computer and use it in GitHub Desktop.
Kotlin forEach, map, filter operations learned from https://developer.android.com/codelabs/basic-android-kotlin-training-collections
fun main() {
val peopleAges = mutableMapOf<String, Int>(
"Fred" to 30,
"Ann" to 23
)
peopleAges.put("Barbara", 42)
peopleAges["Joe"] = 51
peopleAges["Fred"] = 31
peopleAges.forEach { print("${it.key} is ${it.value}, ") }
println()
println(peopleAges.map { "${it.key} is ${it.value}" }.joinToString(", ") )
println()
println(peopleAges)
val filteredNames = peopleAges.filter { it.key.length < 4 }
println(filteredNames)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment