6 ms·
If you want insertion order you need something like a LinkedHashMap. Java's had that forever, but it's not really a common use case.
by yarg 2y ago
If you want insertion order you need something like a LinkedHashMap.
Java's had that forever, but it's not really a common use case.
- rezaprima 2y agofirst time I know of LinkedHashMap is by reading a JSON library (Jason, iirc).
- matwood 2y agoIf you spend any amount of time programming Java, reviewing this list is a big help. https://www.geeksforgeeks.org/collections-in-java-2/ https://www.geeksforgeeks.org/collections-in-java-2/
- SkiFire13 2y agoI personally prefer something like Rust's `indexmap` instead, which is basically a hash table mapping from key to an index into a `Vec` containing the values. AFAIK this is also the approach taken by C#'s Dictionary.
- KMag 2y agoI believe one rewrite of Python's dict was the first mainstream use of this sort of hash map as a default implementation. I wish they provided a sort method to re-sort and re-index the vector to change the iteration order without the space overhead of creating a and sorting a separate vector/list of keys (or key-value pairs, depending on use case. You might want to change iteration order based on the currently held value).
- masklinn 2y ago> I believe one rewrite of Python's dict was the first mainstream use of this sort of hash map as a default implementation. Technically pypy and I believe php implemented this model first, though it's probably most well known from cpython (for which it had been proposed first, by raymond hettinger).
- lifthrasiir 2y agoI believe PHP is one of the (much) earlier languages with them.
- andrewshadura 2y agoTcl was much earlier, in fact.