Learn the Basics of the Kotlin Language

May 22 2024 · Kotlin 1.9, Android 14, Kotlin Playground 1.9

Lesson 06: Use Collections

Demo 2

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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)
}

Mutable Sets

Use mutableSetOf to initialize a mutable Set:

fun main() {
  val oceans = mutableSetOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
}
fun main() {
  val oceans = mutableSetOf("Atlantic Ocean", "Indian Ocean")
  oceans.add("Pacific Ocean")
  oceans.addAll(listOf("Southern Ocean", "Arctic Ocean"))
  println(oceans)
}
fun main() {
  val oceans = mutableSetOf("Atlantic Ocean", "Indian Ocean")
  oceans.add("Pacific Ocean")
  oceans.addAll(listOf("Southern Ocean", "Arctic Ocean"))
  oceans.add("Pacific Ocean")
  println(oceans)
}
fun main() {
  val oceans = mutableSetOf("Atlantic Ocean", "Indian Ocean")
  println(oceans.elementAt(0))
}
fun main() {
  val oceans = mutableSetOf("Atlantic Ocean", "Indian Ocean")
  oceans.remove("Atlantic Ocean")
  println(oceans)
}
fun main() {
  val oceans = emptySet<String>() // You need to specify the data type for the items the set could contain.
}
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.

fun main() {
  emptyMap<Int, String>()
}
fun main() {
  val techBlogs = mapOf(1 to "TechCrunch", 2 to "Engadget", 3 to "The Verge", 4 to "Mashable")
  println(techBlogs)
}
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)
}
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}")
  }
}

Arrays

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())
}
fun main() {
  emptyArray<String>()
}
fun main() {
  val books = Array<Int>(3) { 0 }
  println(books.toList())
}
fun main() {
  val books = Array<Int>(3) { it * 2 }
  println(books.toList())
}
fun main() {
  val multiplatforms = arrayOf("Android", "iOS", "Web", "Desktop", "Server")
  println(multiplatforms[2])
}
fun main() {
  val multiplatforms = arrayOf("iOS", "Web", "Desktop", "Server")
  multiplatforms[0] = "Android"
  println(multiplatforms.toList())
}

Comparing Arrays

To 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:

fun main() {
  val multiplatformsA = arrayOf("iOS", "Web", "Desktop", "Server")
  val multiplatformsB = arrayOf("iOS", "Web", "Desktop", "Server")
  println(multiplatformsA.contentEquals(multiplatformsB))
}

Collection Elements

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)
}
See forum comments
Cinema mode Download course materials from Github
Previous: Demo 1 Next: Conclusion