Back
Close

Kotlin Starter Pack

romainbsl
18.4K views

Collections

It's important to understand that Kotlin's collection are built on top of the Java API. That means a lot for interoperability.

But, Kotlin brings consistency and simplicity.

List of ...

// { autofold
fun main(args: Array<String>) {
//}
val numbers = listOf(1, 2, 3)
// { autofold
numbers.forEach { println(it) }
}
//}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Set of ...

// { autofold
fun main(args: Array<String>) {
//}
val numbers = setOf(1, 2, 2, 3, 4, 3)
// { autofold
numbers.forEach { println(it) }
}
//}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Map to ...

// { autofold
fun main(args: Array<String>) {
//}
val map = hashMapOf(1 to "first", 2 to "second", 3 to "third")
// { autofold
map.forEach {(key, value) -> println("key: $key ; value: $value")}
}
//}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Note that beyond that simplicity, Kotlin provides some cool features, essentially based on extensions of the Java API.

Here a sample of what Kotlin is capable of. You may find more into the Kotlin documentation.

// { autofold
fun main(args: Array<String>) {
//}
val numbers = listOf(1, 2, 3)
// forearch with indexes
numbers.forEachIndexed { index, it -> println("At index $index there is $it") }
// first()
println("numbers' first is: ${numbers.first()}")
// last
println("numbers' last is: ${numbers.last()}")
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants