Rohde Fischer
11/22/2022, 3:54 PMSortedMap
? E.g., when I'm deserializing a json
map where the order is important 🙂 It seems that Map
happens to be sorted, but I assume it's the same as for Java - there is no guarantee it will stay that way, am I correct in that assumption? If so what can/should I do?ephemient
11/22/2022, 4:09 PMephemient
11/22/2022, 4:10 PM.toSortedMap()
extension function, which returns a java.util.SortedMap
. this is a map whose keys are always iterated in comparator order. this is not available on Kotlin/Nativeephemient
11/22/2022, 4:12 PMMap
-returning functions in Kotlin stdlib return an order-preserving map: entries are iterated in insertion order (kotlin.collections.HashMap
being an exception and explicitly guaranteeing no particular order)ephemient
11/22/2022, 4:14 PMSortedMap
, e.g.
mapOf("b" to 1, "a" to 2).toSortedMap().entries.toList() // [a=2, b=1]
or do you want an order-preserving map, e.g.
mapOf("b" to 1, "a" to 2).entries.toList() // [b=1, a=2]
ephemient
11/22/2022, 4:17 PMMap
deserialization uses LinkedHashMap
, which preserves orderRohde Fischer
11/22/2022, 4:30 PMephemient
11/22/2022, 4:39 PMMichael Paus
11/22/2022, 5:19 PMThe returned map preserves the entry iteration order.
Rohde Fischer
11/22/2022, 5:20 PMephemient
11/22/2022, 5:27 PMMichael Paus
11/22/2022, 6:31 PMRohde Fischer
11/23/2022, 7:02 AM