Learn the Basics of the Kotlin Language

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

Lesson 06: Use Collections

Demo 1

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

A collection is a group of items. Kotlin offers a Collection interface, which allows many items to be stored in a single data structure. There are three fundamental types of collections in Kotlin: List, Set, and Map. The first type that you’ll learn about is List. Start a new Kotlin Playground session to follow along with the demo. Or you could use any other Kotlin programming environment of your choice.

List

A list maintains items in the order of their addition or creation, making it a useful feature to bear in mind.

fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans)
}

Indexing

In Kotlin, as well as most other programming languages, you access the elements in a collection using a zero-based index. This means that the first item in the list has an index of 0, not 1. This might seem counterintuitive. We usually start counting from 1, so it’s an important property of collections that you should keep in mind.

fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans[0])
}

Mutable Lists

In Kotlin, collections can either be mutable or immutable. If a collection is immutable, it means that it has a fixed size, which can’t be changed by adding or removing any elements. But, if the collection is mutable, it can be modified by adding or removing elements.

fun main() {
  val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
}
fun main() {
  val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
  println(oceans)
  oceans.add("Indian Ocean")
  println(oceans)
}
fun main() {
  val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
  println(oceans)
  oceans.removeAt(2)
  println(oceans)
}

Immutable Lists

The immutability of a list applies to the elements within the collection and not the variable or object that holds the list. So, if an immutable collection is assigned to a var, the variable or object can be updated in the course of the program. Make oceans a var, and change mutableListOf to listOf. Then reassign a new list to oceans:

fun main() {
  var oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
  println(oceans)
  oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans)
}
fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
  println(oceans)
  oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans)
}
fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
  oceans.add("Indian Ocean") // Not possible
  println(oceans)
}
fun main() {
  val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
  println(oceans)
  oceans.add("Indian Ocean")
  println(oceans)
}
fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println("There are ${oceans.size} oceans in the world.")
}
fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  for (ocean in oceans) {
    println(ocean)
  }
}

Comparing Lists

To compare two lists, use the equality operator ==. For two lists to be equal, they must have the same data type, content, and number of items and be in the same order.

fun main() {
  val oceans1 = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  val oceans2 = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans1 == oceans2)
}

Sequences

Sequences and collections function differently. While collections hold data, sequences produce items as required. In Kotlin, sequences are represented by the Sequence interface. To create an empty sequence, you can use the emptySequence() function, specifying the type used in the Sequence:

fun main() {
  emptySequence<String>()
}
fun main() {
  val weekdaysSequence = sequenceOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  println(weekdaysSequence.toList())
}
fun main() {
  val weekdaysList = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  val weekdaysSequence = weekdaysList.asSequence()
  println(weekdaysSequence.toList())
}
fun main() {
  val evens10to20 = generateSequence(10) { it + 2 }
  println(evens10to20.take(6).count())
  println(evens10to20.take(6).toList())
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 3 Next: Demo 2