I want to make sure I understand the docs. Here says that Map preserves the order of elements insertion. What I understand is that when I use
mapOf(…)
or
mutableMapOf(…)
, the order in which elements are inserted are preserved. Is that a correct statement?
k
Kamil Kalisz
04/25/2020, 4:19 AM
well Map interface is not preserving order. One of implementation (LinkedHashMap) is preserving order (and it’s default one). Personally when I need ordering I’m using LinkedHaspMap explicitly to show my intentions in source code and to guarantee sorting.
linkedMapOf
g
Gilberto Diaz
04/27/2020, 2:17 PM
But that’s not what the docs stated.
Copy code
The default implementation of Map – LinkedHashMap – preserves the order of elements insertion when iterating the map. In turn, an alternative implementation – HashMap – says nothing about the elements order.