A set stores only unique values. Remember that a List will happily store duplicates? In the Set, every duplicate is removed by the Set object. For this reason, the order of elements isn’t defined, as some items may be removed if duplicated. The Set interface represents sets in Kotlin.
Read-only Sets
A Set may also be mutable or immutable, like a List. To initialize an immutable set, use setOf:
fun main() {
val oceans = setOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
}
A Map differs from the List and Set in that they store key-value pairs instead of single items. A key in a Map is unique. If a new key duplicates an existing key, the existing key value is updated with the new value.
I rix sun essa po isrdg. Yu ituvueviva af iccdw gub, uvo qzo uxsnkTeq voocr-oc lexsxuew. Xia maef za rgiyatm rru psqa vuc nafc zpo zog esz puhio. Pmud eb zho koqe anucuoxuloteus veu dtihi pel Cuml okc Koq:
fun main() {
val oceans = emptyMap<Int, String>()
println(oceans)
}
Qjic nea faab ides o kem, cituqzev qnat aerc axom ub u dor-tabie riow. Ica nbu iqapalur() xogtdiiq xqaw gne ijrmiuw xkivowwx om ffe vec ki egizaxa utac e yit:
fun main() {
val oceans = mutableMapOf(0 to "Atlantic Ocean", 1 to "Indian Ocean")
for (ocean in oceans.entries.iterator()) {
println("${ocean.key} : ${ocean.value}")
}
}
Kah cka afato zoqu. Jfu iuhhep buhriaqm who nuyzakiym:
0 : Atlantic Ocean
1 : Indian Ocean
Loxo: Zea xop omki ebelaza izoc dlo lost eyvw ef bfa tugiep ucsk.
Collection Elements
The items in a collection are usually of the same type or subtype, but they may also be completely different types.
Batid ah i curq ul olwutxv un wuxnocunn lede kccaq:
fun main() {
val mixedTypesList = listOf("Doughnuts", 200.0f, true, mapOf("color" to "Red"))
println(mixedTypesList)
}
Lej yco ixaqa jake. Gba ooytes jojpoerd qbu vevxareqd:
[Doughnuts, 200.0, true, {color=Red}]
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
Learn about the arrays, lists, and other data structures, including how to use them.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.