I need a value of Someclass based on the key. And the key can be a string, boolean, or another Object, that’s why I used Object as key. But I have a problem, when the object is a string. and I have two Object of string, which is equals, but it should return different value, because it is a different object.
The code that I have:
Object k = new String("action");
Object l = new String("action");
Hashtable<Object,SomeClass> map = new Hashtable<Object, SomeClass>();
map.put(k,anObject1);
map.put(l,anObject2);
map.remove(k); // it is removing both with k and l.
when I check the hashCode() of both object, it returns the same value, which is ultimately what not I want.
Is there any solution of this? Is I need to make a new class that override Equals() of object? but, still, the hashCode. 🙁 The problem is I need a hashCode that return different value for different Object.
Edited:
I am doing this because I need to do different action based on what the string is, but the action will differ by the value returned by map with the key.
Updated:
Okay, here is all the story why I need this weird thing.
I have a player instance, and 3 land instances. So I wanted the player to plow land1, land2, land3. If the player wanted to plow a land, that land make a running thread, that tell the player to move to position X, and do the job action, and wait() by object action, and when the other thread notify this thread by object action, the land then modify itself. The player then make an animation based on the object action. I am having ArrayList<Position> destination and ‘ArrayList action` to have it. Maybe you can read my other question about this here.
So I wanted to make the action cancelable. I implements it by also passing the action object. I have a button that show for every action, and every button will cancel that action. I passing the action here too. So when I click the button, the land will get the notify. The problem is I can’t make the ArrayList<Position> remove the destination by the Action, because it’s don’t know where the index. I’m new to Java but have been using C++ a lot, so I thinking of using Hashtables, because it’s O(1) differs with C++ O(log n), and kind of convenience because not many changes to my current code.
Is it understandable?
You could use an IdentityHashMap instead, though it might help if you could explain what exactly you’re trying to do here. This is not a terribly common requirement and unless you have some very good reasons you want to do this, there’s probably something else you’re better off doing.
By the way, a
Mapcan only hold one value for a single key. So after your second call tomap.put, the first value you put in there is already gone.Edit:
Ok, I’ve read your explanation and… well, I’m still not completely clear on what you’re doing. Here’s my best guess from what you’ve said:
String.String“plow” to thePositions of the 3 different lands.If this (or something like it) is what you’re trying to do, here are my thoughts:
Stringshould not be unique… “plow” is “plow”, whatever land it is done on. What is unique is the combination of an action like “plow” and the land that action is to be done on.enumor some such to represent action types.Actionclass should contain an “action type” and aPosition. When you click to cancel thatAction, you have the data you need right there.