I am trying to code a Graph in java and one of my member variables is a map that maps strings to integers. The integers are the indexes of a List<List<Integer>>. Lets say I have a graph like
"Alpha" maps to 0,
"Beta" maps to 1,
"Charlie" maps to 2,
"Delta" maps to 3.
If I delete Beta from my graph, I want to shift Charlie and Delta down. But, the Java map function can only get the values not the keys, so I can’t change the mappings. How should I go about doing this?
Use LinkedHashMap. There the insertion order of the keys are maintained.
If you want to maintain keys in an alphabetical order (or any other way), then use TreeMap. In this case, once you delete an entry, keys will sort themselves according to the rule (e.g. alphabetical order). The beauty of TreeMap is you can define the sorting rule here.
But, in any case HashMap is not a proper choice for you.