#How to insert at index in ArrayHashMap

1 messages · Page 1 of 1 (latest)

visual dew
#

It seems that array hashmap has arbitrary ordering (or am I misunderstanding?)
If so, how do I insert at a specific index?
Thanks

warm kindle
#

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:

  1. fast retrieval of values given keys
  2. fast iteration on key-value pairs in a specific order that is not insertion order.
visual dew
#

Thanks for offering solutions, I guess i'll see when I actually need to do insertion, i was curious how it would be done

warm kindle
#

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.

visual dew
#

Indeed!