I am trying to use a HashMap to map a unique string to a string ArrayList like this:
HashMap>
Basical
HashMaps are not ordered, unless you use a LinkedHashMap
or SortedMap
. In this case, you may want a LinkedHashMap
. This will iterate in order of insertion (or in order of last access if you prefer). In this case, it would be
int index = 0;
for ( Map.Entry> e : myHashMap.iterator().entrySet() ) {
String key = e.getKey();
ArrayList val = e.getValue();
index++;
}
There is no direct get(index) in a map because it is an unordered list of key/value pairs. LinkedHashMap
is a special case that keeps the order.