#How to insert at index in ArrayHashMap
1 messages · Page 1 of 1 (latest)
Assuming you don't remove, iteration on an ArrayHashMap is not arbitrary, it follows the insertion order.
If you need a different order, maybe you could insert all key-value pairs in a separate ArrayList using insert() and then insert them normally in the ArrayHashMap, they will be in the order you want.
Note that insert()ing in the middle of an ArrayList is much much slower than append()ing. Alternatively, if you can define a suitable comparison operator, append() the pairs to the ArrayList and sort it.
Are you sure you need this? I struggle to imagine a use case where you want both:
- fast retrieval of values given keys
- fast iteration on key-value pairs in a specific order that is not insertion order.
I have a layered state system for my game where i need to iterate for the draw/update routines, and retrieve using a name to access other states
That also includes addding and removing states but I am not sure I 100% need arbitrary insertion indeed
Thanks for offering solutions, I guess i'll see when I actually need to do insertion, i was curious how it would be done
maybe you don't want an ArrayHashMap, maybe you can split your data into a carefully ordered ArrayList, and a separate HashMap for the name->routine mapping.
Indeed!