Building immutable collection dynamically in Kotlin
- Cyril Sahula
- Jan 10
- 1 min read
Updated: 4 days ago
When and Why to Use Kotlin Collection Builders
If there's a requirement to dynamically construct a collection based on certain conditions, two main approaches are available in Kotlin:
Using a mutable collection directly
Employing a collection builder function (e.g., buildSet, buildList, buildMap)
The collection builder approach still uses a mutable collection internally, but the benefit lies in its encapsulation. This leads to cleaner and more concise code, and the final result is an immutable collection.
This post is short and clear although I have met so much mutable collection in projects that is nice to remain it.
MUTABLE WAY
It works but collction must be muttable and code is less fluent.
fun mapToWorkTypes(externalTypes:Â List<String>):Â Set<WorkType>Â {
val workTypes:Â MutableSet<WorkType>Â =Â mutableSetOf()
if (externalTypes.contains("#001")) workTypes.add(EMPLOYEE)
if (externalTypes.contains("#002")) workTypes.add(CONTRACTOR)
if (externalTypes.contains("#003")) workTypes.add(SEASONAL)
if (externalTypes.contains("#004")) workTypes.add(INTERN)
if (externalTypes.contains("#005")) workTypes.add(CONTRACTOR)
if (workTypes.isEmpty()) workTypes.add(EMPLOYEE)
return workTypes.toSet()
}
IMMUTABLE WAY
The code below uses collection builder. Check documentations of collection builders buildSet, buildList, buildMap.
fun mapToWorkTypes(externalTypes:Â List<String>)Â =
buildSet {
        if (externalTypes.contains("#001")) add(EMPLOYEE)
if (externalTypes.contains("#002")) add(CONTRACTOR)
if (externalTypes.contains("#003")) add(SEASONAL)
if (externalTypes.contains("#004")) add(INTERN)
if (externalTypes.contains("#005")) add(CONTRACTOR)
ifEmpty {Â add(EMPLOYEE)Â }
    } Â
Conclusion
Using a mutable collection directly requires more boilerplate and an explicit conversion to an immutable form via .toSet(). It also increases the risk of logic errors, such as accidentally checking or modifying the wrong collection (workTypes vs. externalTypes).
In contrast, collection builders provide a cleaner and more concise alternative:
Less boilerplate
Clearer logic
Automatically return immutable collections
Encourage encapsulated and expressive code
Whenever possible, prefer collection builders for constructing collections based on dynamic conditions.