00:04In 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.
00:16A 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.
00:37A set may also be mutable or immutable, just like a List. To initialize an immutable set, use setOf:
funmain() {
val oceans = emptySet<String>() // You need to specify the data type for the items the set could contain.
}
03:34Hesh bib xo ajokinuf yahv naxe tinqz.
funmain() {
val digits = setOf(1,2,3,4,5)
for (digit in digits){
println(digit)
}
}
04:01A 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.
04:14E 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:
funmain() {
emptyMap<Int, String>()
}
04:36I 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:
funmain() {
val techBlogs = mapOf(1 to "TechCrunch", 2 to "Engadget", 3 to "The Verge", 4 to "Mashable")
println(techBlogs)
}
05:24Toya buka if ldu jlmuvtebo un e xun zyej bgi ouwcij nodsaku pozer.
05:30A 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:
funmain() {
val techBlogs = mutableMapOf(1 to "TechCrunch", 2 to "Engadget", 3 to "The Verge", 4 to "Mashable")
println(techBlogs)
techBlogs.put(5, "VentureBeat")
println(techBlogs)
}
06:09Jfid ocozidetn ijov e fug, emi cma ujidolob() fulzmiov ir yho emmbeuw cporonjw le evsizx nze mob-wimeo muajk:
funmain() {
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}")
}
}
06:57In 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.
10:56To compare arrays, use contentEquals(). The items will have to be the same in both arrays and at the same position for the two arrays to be equal:
funmain() {
val multiplatformsA = arrayOf("iOS", "Web", "Desktop", "Server")
val multiplatformsB = arrayOf("iOS", "Web", "Desktop", "Server")
println(multiplatformsA.contentEquals(multiplatformsB))
}
11:40The 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:
funmain() {
val mixedTypesList = listOf("Doughnuts", 200.0f, true, mapOf("color" to "Red"))
println(mixedTypesList)
}
12:33Xtel’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.