So, I'd like to use a java map where the keys are an object...but rather than keying on the object's value, they key on the object ID. So, something like the following would be totally valid code:
Map<String, Integer> map = new HashMap<String, Integer>();
String s1 = "hi!";
String s2 = "hi!";
map.put(s1, 10);
map.put(s2, 47);
Is this possible? Is there a simple way to do this without building an object ID or something overly cumbersome in my class? Basically, I need a way to associate an ever-changing list of values with a given object. This list of values will potentially be different for objects that have the same value, hence why the default map doesn't work. Other than refactoring my class to do this myself (not really an option, given the time) is there anything that I could use?
Thanks.
EDIT: Further information.
The example above was just an example. What I will be using this for is the implementation of a Uniform-Cost search algorithm. For any given node in a search with this algorithm, one must also have the path that has been taken so far. The reason a value-based hash map doesn't work is that this algorithm can reiterate over already-explored nodes. The paths would be different at this point, although the value of "where am I now?" is identical.
equals()andhashCode()by yourself, you're basically inheritingObjects implementation, which compare by identity, not equality.equals()andhashCode()so that it compares by equality. Otherwise none of my string based maps would work :(Stringabsolutely overridesequals()andhashCode().in my class. As far as I understand, theStrings were only given as examples.