For LinkedHashMap, the answer is pretty clear that it preserves the order of insertion.
But for ListMap, it seems that there are some confuses here.
Firstly, there are two ListMap.
- scala.collection.mutable.ListMap
- scala.collection.immutable.ListMap.
Secondly, the document for ListMap has something wrong as far as I tried.
mutable.ListMap
The actual order is not the insertion order as it says.
And it is not the inverse order of insertion, either. The result I tried is [forth, second, first, third]
A simple mutable map backed by a list, so it preserves insertion order.
immutable.ListMap
As the document saying that, the order is the insertion order.
One thing to notice is that it is stored internally in reversed insertion order. And the internally stored order and the iterable/traversal order are two things. The internally stored order decides the time complexity of lookup methods such as head/last/tail/init/.
This class implements immutable maps using a list-based data structure. List map iterators and traversal methods visit key-value pairs in the order whey were first inserted.
Entries are stored internally in reversed insertion order, which means the newest key is at the head of the list.
ListMap
is only immutable, it's also hard to "find" like if you make it part of another nested type even for aval
, it needs to be imported explicitly. It's not clear to me why it isn't already in scope likeList
andMap
are. – Stoup