开发者

Java Hashtable<Object, Someclass>, if the key is string? if the string is same, it will remove all value with the same string

开发者 https://www.devze.com 2023-02-23 09:36 出处:网络
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 usedObject as key. But I have a problem, when the object is a string. and I have tw

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, 开发者_JS百科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 Map can only hold one value for a single key. So after your second call to map.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:

  • You have an action called "plow", represented as a String.
  • You are telling the player to "plow" 3 different lands.
  • You want to be able to cancel that action for one land without cancelling it for the others.
  • To do this, you're trying to map 3 different instances of the String "plow" to the Positions of the 3 different lands.

If this (or something like it) is what you're trying to do, here are my thoughts:

  • The action String should 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.
  • Given that, "plow" should be considered something like an "action type". It might be good to use an enum or some such to represent action types.
  • An Action class should contain an "action type" and a Position. When you click to cancel that Action, you have the data you need right there.


The String class overrides equals() and hashCode() such that two Strings with the same characters have the same hashCode() and are equal. But the Object class itself has an equals() method that never returns true for two different objects. It's actually not necessary (nor possible!) for hashcodes to be unique; they just have to be nicely distributed across the range of possible values.

So in any case: String is ill-suited for your requirements, I agree; but an object of any other class that doesn't override equals() and hashCode() should be just fine.


This is the very semantics for a Hashmap: keys are uniqe and each value from the key domain maps to at most one value in the Hashmap.

Think of it like an array, only that the array is not indexed by numbers but by arbitrary objects. You can't have more than one different value at the same index in an array.


You might consider creating your own class to represent a Key and define your own hashcode by some unique identifier .. perhaps using a AtomicLong

0

精彩评论

暂无评论...
验证码 换一张
取 消