Data Classes in Collections and as Map Keys
Use data classes in sets and maps leveraging structural equality.
Data Classes in Collections and as Map Keys is a free Kotlin Academy lesson on CoddyKit — lesson 4 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 Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Data Classes as Value Objects
Regular Class: Bad Map Key
class Key(val id: Int) // no equals/hashCode
val map = mutableMapOf<Key, String>()
val k1 = Key(1)
val k2 = Key(1) // same id, different object
map[k1] = "value"
println(map[k2]) // null! k2 != k1 by referenceData Class: Correct Map Key
data class Key(val id: Int)
val map = mutableMapOf<Key, String>()
val k1 = Key(1)
val k2 = Key(1)
map[k1] = "value"
println(map[k2]) // "value"! Equal by valueData Class in a Set
data class Tag(val name: String)
val tags = mutableSetOf<Tag>()
tags.add(Tag("kotlin"))
tags.add(Tag("kotlin")) // duplicate
tags.add(Tag("java"))
println(tags.size) // 2 (not 3)GroupBy with Data Classes
data class Category(val name: String, val parent: String)
val items = listOf(
"List" to Category("Collections", "stdlib"),
"Map" to Category("Collections", "stdlib"),
"Flow" to Category("Coroutines", "kotlinx")
)
val grouped = items.groupBy { it.second }
println(grouped.keys.size) // 2Counting Occurrences with Map
data class Pair2(val x: Int, val y: Int)
val points = listOf(Pair2(1,2), Pair2(1,2), Pair2(3,4))
val counts = points.groupingBy { it }.eachCount()
println(counts) // {Pair2(x=1, y=2)=2, Pair2(x=3, y=4)=1}Nested Data Classes
data class Address(val city: String, val country: String)
data class Person(val name: String, val address: Address)
val p1 = Person("Alice", Address("Berlin", "DE"))
val p2 = Person("Alice", Address("Berlin", "DE"))
println(p1 == p2) // truesortedBy with Data Classes
data class Student(val name: String, val grade: Double)
val students = listOf(
Student("Bob", 3.2),
Student("Alice", 3.8),
Student("Charlie", 2.9)
)
println(students.sortedBy { it.grade })Data Classes and Caching
data class CacheKey(val userId: Long, val resourceType: String)
val cache = HashMap<CacheKey, ByteArray>()
val key = CacheKey(42, "avatar")
cache[key] = byteArrayOf(1, 2, 3)
println(cache[CacheKey(42, "avatar")] != null) // truedistinct on Data Classes
data class Event(val type: String, val ts: Long)
val events = listOf(
Event("click", 1000),
Event("click", 1000), // duplicate
Event("scroll", 2000)
)
println(events.distinct().size) // 2Immutable Keys Are Safer
data class ImmutableKey(val id: Int) // val properties only
// DON'T: data class MutableKey(var id: Int) // changing id breaks map!Quick Check
Recap
Frequently asked questions
Is the “Data Classes in Collections and as Map Keys” lesson free?
Yes — the full text of “Data Classes in Collections and as Map Keys” is free to read here on the web, and the Kotlin 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 Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Data Classes in Collections and as Map Keys”?
Use data classes in sets and maps leveraging structural equality. You practise Kotlin 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 Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Data Classes in Collections and as Map Keys” 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 Kotlin Academy lesson?
Yes. Every Kotlin 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
- Data Class Basics: Auto-generated Methods
- Copying with copy() and Partial Overrides
- Destructuring Declarations with componentN
- Data Classes in Collections and as Map Keys