3 ms·
> The most important thing is that there is some defined order. This makes it easier to debug, and reproducibility is just a very nice property. See https://en.
by sgeisenh 5y ago
> The most important thing is that there is some defined order. This makes it easier to debug, and reproducibility is just a very nice property. See https://en.wikipedia.org/wiki/Reproducible_builds https://en.wikipedia.org/wiki/Reproducible_builds Simple stuff like making golden output files for your tests becomes easier.
Relying on this type of behavior when it isn't required is somewhat of an anti-pattern. And can often lead to mindlessly copying the results of an implementation in tests.
A quick aside: any sort of generic ordered container that supports O(n) iteration must necessarily require O(n log n) work to construct in the average case. So the asymptotic complexity is necessarily the same as sorting the keys of a hash map with undefined iteration order to artificially create a defined order.
It isn't too hard to simulate the behavior of insertion order iteration by maintaining an array alongside the hash map. And surprisingly enough, space and runtime complexity is practically identical to containers that implement insertion order iteration ;-)
- ErikCorry 5y ago> any sort of generic ordered container that supports O(n) iteration must necessarily require O(n log n) work to construct in the average case This doesn't apply if the order you want is insertion order. These hash maps are constructed in O(n) on average, just like any other hash maps. Java's LinkedHashMap is also constructed in O(n).
- sgeisenh 5y ago"ordered container" meaning that the container is ordered based on the objects it contains.