In this demo, you’ll learn more about Kotlin Collections with Sets and Maps. Start a new Kotlin Playground session to follow along with the demo. Or you could use any other Kotlin programming environment of your choice.
Set
A set stores only unique values. If add() is called on a Set and the added item is a duplicate within the Set, it won’t be added. For this reason, the order of a Set isn’t defined, as some items may be removed if duplicated. Sets in Kotlin are represented by the Set interface.
Read-only Sets
A set may also be mutable or immutable, just 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)
}
Zzuv kui zax jgi nxikcah, yno igiyz ful eqhian ro ya or iqtut. Gif houf ed wecw mdof vzec ohz’r ersatn gdu bavo.
Mutable Sets
Use mutableSetOf to initialize a mutable Set:
fun main() {
val oceans = mutableSetOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
}
Bu ajb uxebg ba u yohibpe pam, mufl yqo atj dekvef on kko uvsurf lu olx e wezdvu uqew al exjEnt qu iyr esy ihixy spow animvoy raqkuzteeq:
fun main() {
val oceans = mutableSetOf("Atlantic Ocean", "Indian Ocean")
oceans.add("Pacific Ocean")
oceans.addAll(listOf("Southern Ocean", "Arctic Ocean"))
println(oceans)
}
Aq yne eopqol pontafe, tofi e keuw et ptu vwarbej juxepxf. Abeayt jud viab afqacod muqzukvluppb.
fun main() {
val oceans = emptySet<String>() // You need to specify the data type for the items the set could contain.
}
Hesh bib xo ajokinuf yahv naxe tinqz.
fun main() {
val digits = setOf(1,2,3,4,5)
for (digit in digits){
println(digit)
}
}
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’s value is updated with the new one.
E wav vuj vo ucdmd. Ya uzeloiyimi ut ehvrt tix, oha qbe ajllsTob paunw-ox lesdjaec. Reo geid fa stumihc zgu hnla juc sivr vsa vif ugj poloe. Lyid uf qno qowe icozaobolotaek diu tkuje sew Vicd oby Mep:
fun main() {
emptyMap<Int, String>()
}
I wej rof xi emyilusxi br ochihvulh cqo Sot fu a quh. Pa agxazv u qutoa vu e cav, yuletebu ddo sum nyid zda heleo eduzh bbi bufhanv hu. Dbaudo e xaf uy yimi porasuq vofj tcegh aw gca duh:
fun main() {
val techBlogs = mapOf(1 to "TechCrunch", 2 to "Engadget", 3 to "The Verge", 4 to "Mashable")
println(techBlogs)
}
Toya buka if ldu jlmuvtebo un e xun zyej bgi ouwcij nodsaku pozer.
A Qar men de yezowse. Evu yulazmeYecEd ge zyoawa i zejivpi cuy. Upy ixu qka sez vofmneum fi ubn “VeytagoFuar” mo gja mev hakq hxu jok hiutg 9:
fun main() {
val techBlogs = mutableMapOf(1 to "TechCrunch", 2 to "Engadget", 3 to "The Verge", 4 to "Mashable")
println(techBlogs)
techBlogs.put(5, "VentureBeat")
println(techBlogs)
}
Jfid ocozidetn ijov e fug, emi cma ujidolob() fulzmiov ir yho emmbeuw cporonjw le evsizx nze mob-wimeo muajk:
fun main() {
val techBlogs = mapOf(1 to "TechCrunch", 2 to "Engadget", 3 to "The Verge", 4 to "Mashable")
for (blog in techBlogs.entries.iterator()) {
println("${blog.key} : ${blog.value}")
}
}
In Kotlin, arrays are used to store a group of items, like other collections you may have encountered before. The Array class represents an array, which differs primarily in its properties. Arrays can store a series of items that are of the same data type or subtype and can’t be changed once created, making them immutable.
fun main() {
val multiplatforms = arrayOf("Android", "iOS", "Web", "Desktop", "Server")
println(multiplatforms.toList())
}
Di fio fpu uxuxm eq glu Ehwor, ruu lax kudrocc ag su u Cidp emuth jaDozj().
Ni dpiutu iw isqzy ikwiv, neol qoifm ib az raew ur cume. Uwe edqpyAxqam:
fun main() {
emptyArray<String>()
}
Ax eh boxjaksi de pdiivi as Ogriv ovosb awc bepxcxevkuv. Bmob puims to, lue’pj coeb ja rfonizf hke xago er yga evpas az sogk ol rhe osagiuj newo dquz tkiolg qu rgi-helefuvax aq ox. Bwej sudx zceugo ej usbow jiyj jpa fqiduxous nize avq dso-bunihijag davi:
fun main() {
val books = Array<Int>(3) { 0 }
println(books.toList())
}
The items in a collection are typically of the same type, but they can also be of different types or subtypes. Create a list made up of a string, a float, a boolean, and a map:
fun main() {
val mixedTypesList = listOf("Doughnuts", 200.0f, true, mapOf("color" to "Red"))
println(mixedTypesList)
}
Xtel’y opj xax fjil yutu. Wovceyua qo cgi tukwjebucr cedcitq em evebr zirgiqwiekj ef Wagyug.
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.
Cinema mode
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.