Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
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 ...
1
2
3
4
5
6
7
8
9
// {
fun main(args: Array<String>) {
//}
val numbers = listOf(1, 2, 3)
// {
numbers.forEach { println(it) }
}
//}
Press desired key combination and then press ENTER.
Set of ...
1
2
3
4
5
6
7
8
9
// {
fun main(args: Array<String>) {
//}
val numbers = setOf(1, 2, 2, 3, 4, 3)
// {
numbers.forEach { println(it) }
}
//}
Press desired key combination and then press ENTER.
Map to ...
1
2
3
4
5
6
7
8
9
// {
fun main(args: Array<String>) {
//}
val map = hashMapOf(1 to "first", 2 to "second", 3 to "third")
// {
map.forEach {(key, value) -> println("key: $key ; value: $value")}
}
//}
Press desired key combination and then press ENTER.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// {
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()}")
// sum
println("numbers' sum is: ${numbers.sum()}")
// etc.
// {
}
//}
Press desired key combination and then press ENTER.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content