Instruction 2

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

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

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

Unlock now

Set

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)
}
[Pacific Ocean, Southern Ocean, Arctic Ocean, Atlantic Ocean, Indian Ocean]

Mutable Sets

Here’s an example of the mutableSetOf method, which initializes a mutable Set:

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

fun main() {
  val oceans = emptyMap<Int, String>()
  println(oceans)
}
{}
fun main() {
  val oceans = mapOf(0 to "Atlantic Ocean", 1 to "Indian Ocean")
  println(oceans)
}
{0=Atlantic Ocean, 1=Indian Ocean}
fun main() {
  val oceans = mapOf(0 to "Atlantic Ocean", 1 to "Indian Ocean")
  println(oceans)
  oceans.put(2, "Pacific Ocean")
  println(oceans)
}
Unresolved reference: put
fun main() {
  val oceans = mutableMapOf(0 to "Atlantic Ocean", 1 to "Indian Ocean")
  println(oceans)
  oceans.put(2, "Pacific Ocean")
  println(oceans)
}
{0=Atlantic Ocean, 1=Indian Ocean}
{0=Atlantic Ocean, 1=Indian Ocean, 2=Pacific Ocean}
fun main() {
  val oceans = mutableMapOf(0 to "Atlantic Ocean", 1 to "Indian Ocean")
  for (ocean in oceans.entries.iterator()) {
      println("${ocean.key} : ${ocean.value}")
  }
}
0 : Atlantic Ocean
1 : Indian Ocean

Collection Elements

The items in a collection are usually of the same type or subtype, but they may also be completely different types.

fun main() {
  val mixedTypesList = listOf("Doughnuts", 200.0f, true, mapOf("color" to "Red"))
  println(mixedTypesList)
}
[Doughnuts, 200.0, true, {color=Red}]
See forum comments
Download course materials from Github
Previous: Instruction 1 Next: Instruction 3