Data Classes in Collections
Use in maps and sets.
Data Classes in Collections 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 Meet Collections
Because data classes have value-based equals and hashCode, they behave perfectly inside collections.
This lesson shows how data classes work in lists, sets, and maps.
Data Classes in a List
Store data objects in a list and iterate over them like any other elements.
Their readable toString makes printing easy.
data class User(val name: String, val age: Int)
fun main() {
val users = listOf(User("Alice", 30), User("Bob", 25))
for (u in users) println(u)
}contains Works by Value
Because equals compares values, contains finds an equal object even if it is a different instance.
You do not need the exact same reference.
data class User(val name: String, val age: Int)
fun main() {
val users = listOf(User("Alice", 30))
println(users.contains(User("Alice", 30)))
}Removing Duplicates with a Set
Sets reject duplicates using equals and hashCode.
Two data objects with identical values count as the same element.
data class Point(val x: Int, val y: Int)
fun main() {
val set = setOf(Point(1, 1), Point(1, 1), Point(2, 2))
println(set.size)
}Data Classes as Map Keys
A data class makes an excellent map key because lookups rely on value equality.
You can build a key object on the fly and still find the stored value.
data class Coord(val x: Int, val y: Int)
fun main() {
val grid = mapOf(Coord(0, 0) to "start", Coord(1, 1) to "goal")
println(grid[Coord(1, 1)])
}Data Classes as Map Values
Of course data classes can be the values too, modeling rich records keyed by an id.
This is a common shape for in-memory lookups.
data class User(val name: String, val age: Int)
fun main() {
val byId = mapOf(1 to User("Alice", 30))
println(byId[1]?.name)
}Filtering Collections
Combine data classes with collection functions like filter to query your data.
The lambda accesses properties directly.
data class User(val name: String, val age: Int)
fun main() {
val users = listOf(User("A", 20), User("B", 40))
val adults = users.filter { it.age >= 30 }
println(adults)
}Mapping to Properties
Use map to transform a collection of objects into a collection of one property.
This extracts just the data you need.
data class User(val name: String, val age: Int)
fun main() {
val users = listOf(User("Alice", 30), User("Bob", 25))
val names = users.map { it.name }
println(names)
}Grouping by a Property
groupBy turns a list into a map keyed by a chosen property.
Here users are grouped by their city.
data class User(val name: String, val city: String)
fun main() {
val users = listOf(User("A", "Paris"), User("B", "Paris"), User("C", "Rome"))
val byCity = users.groupBy { it.city }
println(byCity["Paris"]?.size)
}Updating Items Immutably
To update an item in an immutable list, map over it and use copy on the matching element.
The result is a new list with one changed object.
data class User(val name: String, val age: Int)
fun main() {
val users = listOf(User("Alice", 30), User("Bob", 25))
val updated = users.map { if (it.name == "Bob") it.copy(age = 26) else it }
println(updated)
}Putting It Together
Data classes are ideal collection elements:
containsand sets work by value- They make safe, comparable map keys
- They pair naturally with
filter,map, andgroupBy copyenables immutable updates
data class User(val name: String, val age: Int)
fun main() {
val users = setOf(User("Alice", 30), User("Alice", 30))
println(users.size)
println(users.map { it.name })
}Quick Check
Test your understanding of data classes in collections.
Recap
You learned how data classes work in collections:
- Value equality powers
contains, sets, and map keys - They combine with
filter,map, andgroupBy copysupports immutable updates
That completes the Data Classes and Destructuring course.
Frequently asked questions
Is the “Data Classes in Collections” lesson free?
Yes — the full text of “Data Classes in Collections” 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”?
Use in maps and sets. 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” 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
- Declaring Data Classes
- copy and equals
- Destructuring Declarations
- Data Classes in Collections