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. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Data Classes in Collections and as Map Keys” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Data Classes in Collections and as Map Keys”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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