0Pricing
Android Academy · Lesson

Maps and Sets

Key-value and unique collections.

Maps and Sets is a free Android Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Maps and Sets

A map stores key-value pairs: each unique key points to a value, like a dictionary.

A set stores unique items with no duplicates and no guaranteed order.

Both are collection types beyond lists, useful when you need lookups or uniqueness.

Creating a Map

Use mapOf() with key to value pairs to build a read-only map.

Keys must be unique. Here we map fruit names to prices.

fun main() {
    val prices = mapOf("apple" to 3, "banana" to 2)
    println(prices)
    println("Size: ${prices.size}")
}

Looking Up Values

Read a value with square brackets and a key. If the key is missing, you get null.

Use the Elvis operator ?: to supply a default when a key is absent.

fun main() {
    val prices = mapOf("apple" to 3, "banana" to 2)
    println(prices["apple"])
    println(prices["cherry"] ?: "not found")
}

Looping Over a Map

A for loop over a map gives you each entry. You can destructure it into key and value.

This is the usual way to process every pair.

fun main() {
    val ages = mapOf("Ada" to 36, "Bob" to 24)
    for ((name, age) in ages) {
        println("$name is $age")
    }
}

Mutable Maps

Use mutableMapOf() when you need to add or change entries.

Assigning to a new key inserts it; assigning to an existing key replaces its value.

fun main() {
    val stock = mutableMapOf("pens" to 5)
    stock["books"] = 10
    stock["pens"] = 8
    println(stock)
}

Removing from a Map

Call remove() with a key to delete that entry from a mutable map.

Use containsKey() to check whether a key exists before relying on it.

fun main() {
    val m = mutableMapOf("a" to 1, "b" to 2)
    m.remove("a")
    println(m.containsKey("a"))
    println(m)
}

Keys and Values

Every map exposes keys and values collections.

Use them when you only care about one side of the pairs.

fun main() {
    val prices = mapOf("apple" to 3, "banana" to 2)
    println(prices.keys)
    println(prices.values)
}

Creating a Set

Use setOf() to make a read-only set. Duplicate values are automatically dropped.

This makes sets perfect for collecting unique items.

fun main() {
    val tags = setOf("kotlin", "android", "kotlin")
    println(tags)
    println("Size: ${tags.size}")
}

Membership Checks

Sets shine at fast membership checks. Use in or contains() to test if a value is present.

Checking a set is typically faster than scanning a list.

fun main() {
    val allowed = setOf("admin", "editor")
    println("admin" in allowed)
    println(allowed.contains("guest"))
}

Mutable Sets and De-Duping

Use mutableSetOf() to build a set over time. Adding an existing value does nothing.

A quick trick: call toSet() on a list to remove duplicates.

fun main() {
    val nums = listOf(1, 2, 2, 3, 3, 3)
    val unique = nums.toSet()
    println(unique)
}

Map vs Set

Reach for a map when you need to associate keys with values, like a username to a profile.

Reach for a set when you only care whether something exists, with no duplicates allowed.

Quick Check

Test your understanding of maps and sets.

Recap

Maps store key-value pairs with mapOf() / mutableMapOf(); look up by key and handle missing keys with ?:.

Sets store unique items with setOf() / mutableSetOf() and give fast membership checks.

Frequently asked questions

Is the “Maps and Sets” lesson free?

Yes — the full text of “Maps and Sets” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Maps and Sets”?

Key-value and unique collections. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Maps and Sets” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lists and Mutable Lists
  2. Maps and Sets
  3. map, filter, forEach
  4. Lambdas and Higher-Order Functions
← Back to Android Academy