Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

References

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html#kotlin.collections.Map

  1. LinkedHashMap preserves the insertion order (which is similar to dict in Python 3.7+).

val map  = hashMapOf(1 to "x", 2 to "y", -1 to "zz")
println(map)
{-1=zz, 1=x, 2=y}
null

LinkedHashMap

Notice that the order of keys is the same as their insertion order!

val map = linkedMapOf(
    1 to "x", 2 to "y", -1 to "zz"
)
println(map)
{1=x, 2=y, -1=zz}
for ((key, value) in map) {
    println("$key = $value")
}
1 = x
2 = y
-1 = zz
map.forEach { 
    (key, value) -> println("$key = $value") 
}
1 = x
2 = y
-1 = zz
val list: List<String> = map.map { 
    (key, value) -> value
}
list
[x, y, zz]

Count Occurences of Elements in a Collection

listOf(1, 1, 3, 2, 1, 2, 4).groupingBy { it }.eachCount()
Loading...