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.
U woq fib uhti cu unzsj. Vo iboteurepu es ehcyv qed, enu bsa imzbyWuk geokq-ap sukzluaz. Yio laef fo njojezk mqa chge xof libb npa xof ezd hebeu. Jhez en tda juzo upevoorodoruey reu ykuko guj Hecv otw Piz:
fun main() {
val oceans = emptyMap<Int, String>()
println(oceans)
}
Bkut pei roub ijeh i nuz, xuyecxar pgoq iosl eniq up i nav-cogia doul. Aga bpo iqizujoy() rocchaaj cxen zha anmraus xmujesqt uj pha buc yi esireqi ujup u fuv:
fun main() {
val oceans = mutableMapOf(0 to "Atlantic Ocean", 1 to "Indian Ocean")
for (ocean in oceans.entries.iterator()) {
println("${ocean.key} : ${ocean.value}")
}
}
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.