Collections and strings
kotlin
// Collections and strings from the Mini stdlib (Kotlin source compiled together with the program).
data class Person(val name: String, val age: Int)
fun main() {
val xs = listOf(5, 3, 8, 1)
println(xs.map { it * 2 })
println(xs.filter { it > 2 }.sorted())
println(xs.fold(0) { acc, x -> acc + x })
println(xs.zip(listOf("a", "b", "c")))
val people = listOf(Person("Ann", 31), Person("Bo", 25), Person("Cy", 40))
println(people.sortedBy { it.age }.map { it.name })
println(people.maxByOrNull { it.age })
println(people.sumOf { it.age })
val list = mutableListOf(1, 2, 3)
list.add(4)
list[0] = 99
println(list)
val counts = HashMap<String, Int>()
for (w in "a b a c b a".split(" ")) counts[w] = (counts[w] ?: 0) + 1
println(counts)
val set = setOf(1, 2, 2, 3)
println(set.size)
println("Hello, Mini".lowercase().replace("mini", "world").padEnd(15, '.'))
println("a,b,c".split(",").joinToString("|"))
println(buildString {
for (i in 1..3) append(i).append(';')
})
}Output:
text
[10, 6, 16, 2]
[3, 5, 8]
17
[(5, a), (3, b), (8, c)]
[Bo, Ann, Cy]
Person(name=Cy, age=40)
96
[99, 2, 3, 4]
{a=3, b=2, c=1}
3
hello, world...
a|b|c
1;2;3;Notes
- The collections are Kotlin source in
stdlib/(ported from the Kotlin stdlib), compiled together with your program. Only what the program can reach ends up in the generated C. See Standard library. HashMapiteration order matchesjava.util.HashMap, which is why the map prints{a=3, b=2, c=1}exactly as it does on the JVM.- Everything is reference counted. The stdlib hash maps keep before/after links between nodes, which are reference cycles, so a
HashMaporLinkedHashMapleaks its nodes under plain reference counting today.