Ordered maps
Maps don't preserve order the way that lists do. When you push a value to a list, the value will always be in the same position because lists are indexed. With maps, the order of a particular value doesn't matter because you can look it up by its key. When you're iterating over maps, ordering can become a problem though.
Order guarantees
The ordering of list values is guaranteed because lists are an indexed collection. Maps are a keyed collection, which means that the order in which key-value pairs are added isn't guaranteed to be preserved. For example, let's say that I call myMap.set('one', 1).set('two', 2)
. If you were to iterate over this map, one
would probably come before two
. But this is misleading, because there are no ordering guarantees when it comes to iterating over maps. This could be a problem if your application expects iteration ordering to be consistent.
Insertion order is not sort order
The answer to the iteration order problem with maps is to use an ordered map...