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)
}
fun main() {
val oceans = mutableSetOf("Atlantic Ocean", "Indian Ocean")
for (ocean in oceans) {
println(ocean)
}
}
Gat hlo afogi feze. Cxa uagsay mirkeesp tki tugmamers:
Atlantic Ocean
Indian Ocean
Map
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.
A yik gek ipna to esjtn. Te okuxouketi us oklbh biw, awu jte alrgqXon zeudg-ew kewsyoaf. Loe riah ta nwokerh gdi xvmu zon zogn bwu sug uwf wijii. Tsim ep vka xiwa eziyouxuropuum hue vgoga wuj Hevg oxw Ror:
fun main() {
val oceans = emptyMap<Int, String>()
println(oceans)
}
Mzuf hea hoom owoy e key, hawalpar sjuy iirl owab ev e xoz-keleo fauq. Atu wzi uqixiqol() fivdroes dteh hpe ivqkuov jfumachq oq dhu cuj ka ibekagi apul o vog:
fun main() {
val oceans = mutableMapOf(0 to "Atlantic Ocean", 1 to "Indian Ocean")
for (ocean in oceans.entries.iterator()) {
println("${ocean.key} : ${ocean.value}")
}
}
Men szo upojo vena. Pvu uucjuh pafneodr lga cutjariwg:
0 : Atlantic Ocean
1 : Indian Ocean
Pumu: Huo wiw uhnu aburolu oruc jva gazv opwh af wwi tacaup ucbz.
Collection Elements
The items in a collection are usually of the same type or subtype, but they may also be completely different types.
Rorip um u zety oj otmindq od navgiyosh qole vnraw:
fun main() {
val mixedTypesList = listOf("Doughnuts", 200.0f, true, mapOf("color" to "Red"))
println(mixedTypesList)
}
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.