min
09/29/2026, 10:09 AMMarat Akhin
09/29/2026, 10:14 AMLinkedHashMap
also the default map implementations in Kotlin (mapOf / mutableMapOf) preserve the order of insertion (aka are backed by LinkedHashMap)ephemient
09/29/2026, 5:36 PMassociate*, toMap, etc.ephemient
09/29/2026, 5:39 PMHashMap (or specific other concrete types that don't preserve ordering) then of course it won't be ordered by insertionmin
09/30/2026, 7:12 AMmutableMapOf is MutableMap<T> though, which in my book means that the function reserves the right to change which implementor it instantiates in the future. How do I make sure it’s a LinkedHashMap?ephemient
09/30/2026, 7:19 AMThe returned map preserves the entry iteration order.it doesn't matter if it's a
LinkedHashMap or some other type of map that preserves insertion order, but it doesmin
09/30/2026, 7:23 AMLinkedHashMap docs specifically use the term insertion order, which is not what mutableMapOf promises. Also please understand I’m not trying to start a semantic debate in bad faith, I’m just not used to this level of imprecision in documentation and in type signatures.Marat Akhin
09/30/2026, 9:27 AMephemient
09/30/2026, 8:12 PMmapOf() return LinkedHashMap - it can return a singleton instance for the empty map, for example.ephemient
09/30/2026, 8:18 PMMap itself is not ordered, so the only order is the one in which entries (and correspondingly `keys`/`values`) are iterated, which (for the ones produced by stdlib functions) follows insertion order, which is also what LinkedHashMap doesmin
10/01/2026, 7:18 AMmutableMapOf for example returns a MutableMap<K, V>, not a LinkedHashMap<K, V> or a similarly more precise type that locks in the supposed promises.
no interface for "`Map`s which have a consistent iteration order"There’s no reason there couldn’t have been such an interface, not that I think there should be one.
it would be unreasonable to makeI disagree, because multiple possible solutions exist to avoid an unnecessary allocation, employed by other existing languages. • AreturnmapOf()LinkedHashMap
sealed class subtyped by a singleton object that’s empty and a class to be populated.
• A lateinit pointer to the heap buffer (or another means of deferral) that’s not initialised until the map is populated.
And so on. Anyway, I don’t mean to start an argument, and I’ve decided to use a List<Pair<K, V>> instead because I don’t need to look up keys.ephemient
10/01/2026, 8:48 AMephemient
10/01/2026, 8:49 AMList is fine