AFAIK, there is no single data structure that will do this. There is certainly not one in the standard Java collection suite.
Also LinkedHashMap
is not the solution because you cannot efficiently index a LinkedHashMap
.
If you want to do index-based lookup as well as keep-based lookup, solution needs to be a combination of two data structures.
A Map<Key, Value>
and an ArrayList<Value>
is the simpler approach, but it has a couple of problems:
- Insertion and deletion of values from the ArrayList
is expensive, unless you are inserting / deleting at the tail end of the list.
- Insertion and deletion makes the list positions unstable,.
If you want stable indexes and scalable insertion and deletion, then you need a Map<Key, Value>
and a Map<Integer, Value>
... and a way to manage (i.e. recycle) the index values.
The Apache Commons LinkedMap
class is a possible solution, except that it suffers from the problem that index values are not stable in the face of insertions and deletions.