Free tools Windows power users keep installed
One-click scans. No signup required.
In Kotlin, use a data class by default when an entry has a known, stable set of fields. Use a map when the program must choose, look up, or extract properties dynamically—for example, filtering log entries by a property name supplied at runtime. The trade-off is straightforward: maps make flexible lookup easier, but move some mistakes from compile time to runtime.
What problem do maps solve in Kotlin?
Consider a log-processing tool. Each parsed entry might have a timestamp, a level, and a message. A data class describes that fixed shape clearly:
data class LogEntry(
val timestamp: String,
val level: String,
val message: String
)
That representation is convenient when the program knows which properties it will use. The challenge changes when a user can choose a property at runtime—for example, passing a property name on the command line and asking the tool to filter entries by that field.
With a data class, the program can inspect properties using reflection, but turning an arbitrary runtime string into a property access takes extra machinery. A map naturally supports lookup by a key held in a variable, so it fits this dynamic-selection task more directly.
#1 Best Overall
Data classes and maps compared
| Consideration | Data class | Map |
|---|---|---|
| Property names and value types | Known properties and their types are checked by the compiler. | Keys and values are accessed dynamically; missing keys or incorrect value types can fail at runtime. |
| Selecting a property by a runtime name | May require reflection and additional logic. | Direct lookup by a variable key is a natural fit. |
| Implementation trade-off | Simple for a fixed shape, but dynamic access is more cumbersome. | Flexible for changing or selected properties, but often requires casts or a generic accessor. |
| Access performance and memory | The author expects data classes to be faster to read and more space-efficient than sparse maps; no benchmark is reported. | Can represent sparse or varying properties, but the article provides no measured performance or memory figures. |
When a map is the better fit
Maps become useful when the set of properties is not fixed in advance, or when a property name comes from outside the code as a runtime value. In the log example, a parser can retain the common timestamp, level, and message fields while extracting event-specific properties into a map. A filtering command can then look up the selected property without writing a separate accessor for every possible field.
Event-specific keys can be namespaced to reduce collisions—for example, distinguishing a property from one event type from an identically named property in another. Namespacing helps organize keys; it does not restore compiler checks for their presence or types.
Rank #2
What you give up with maps
A data class lets the compiler check that a named property exists and that code uses its declared type. A general map cannot provide the same guarantee for arbitrary keys. A lookup can find no value, and a value retrieved through a generic or cast-based accessor may not have the type the caller expects. Those problems surface when the relevant code runs rather than necessarily at compilation.
This matters most when the data shape is stable and used throughout an application: explicit properties make the contract visible and reduce the scope for misspelled names or invalid casts. For a one-off analysis or a tool whose users select fields dynamically, the flexibility may be worth that cost.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Rank #3
A tentative middle ground: PropertySet
The article proposes a wrapper called PropertySet, parameterized by a marker type, to associate a map with a compile-time label for its intended shape. The idea is to add some structure to a map-based model—a form of gradual typing—without giving up dynamic properties entirely.
This is a proposal, not a proven replacement for data classes: the author explicitly notes that the approach had not been tried in anger. Treat it as an idea to evaluate for a particular design, not as an established Kotlin pattern or a guarantee that map keys and values are safe.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.A practical decision rule
- Choose a data class when the fields are known, stable, and central to your program’s model.
- Choose a map when property names or the property set must be selected or assembled dynamically.
- Keep the boundary clear when parsing dynamic data: validate keys and values before code relies on them as if they were statically typed.
This discussion comes from Duncan’s “Fun With Maps Part 1,” published on 30 June 2019. It presents a conditional design choice, not a claim that maps are generally preferable to data classes.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →

