0

I have a mapping from an int[2] array to weights. So [2, 3] -> 5, [4,5] -> 6 etc. Is it a workable strategy to use a HashMap where I do h.put(Arrays.deepHashCode(a), w)? I have a large amount of data and I want to be able to quickly look up weights given coordinates x,y. I am seeing bugs where h.get(hashcode) seems to be returning me unexpected values later. Not always but occasionally. Is this possibly an artifact of the fact that [x,y] and [a, b] might occasionally have the same deepHashCode()? I will try to boil it down to a small piece of code but currently its hard to isolate this problem.

EDIT: I isolated the problem. Turns out Arrays.deepHashCode([2, 74]) is same as Arrays.deepHashCode([3, 43]) [both return 1097 on my system]. I incorrectly thought that the get() would resolve collisions for me.. but it can't since its mapping a hashcode to a value and the array key is no longer in existence when I did the put().

4
  • 1
    Yes, some code will be very helpful. Also, what do you mean by unexpected values? You seem to imply that you sometimes get the same hashCode for 2 different objects. Is that it? Because that's not disallowed. Commented Jul 4, 2015 at 0:30
  • I store thousands of [x,y] tuples (key) to weight (value) mapping in the hashtable. Later I am reading back [x,y] to get back the weight and its returning unexpected values sometimes (which are not what I entered). You are right that I need to provide a clearer question with code. Will see if I can run a small program to isolate this. I suppose that [x, y] -> w1 can be overwritten by [a,b] -> w2 if [x,y].hashcode and [a,b].hashcode are same but it should be rare. Commented Jul 4, 2015 at 1:04
  • I agree with @sstan. We need to see some code to 1) try to understand the problem and 2) try to figure out why your implementation is not working. Commented Jul 4, 2015 at 1:05
  • See edit above - I isolated the problem but now I need to figure out a better way to allow me to do constant time mappings from a tuple to a weight. Commented Jul 4, 2015 at 2:13

2 Answers 2

2

You should never assume that hash codes are unique. Always remember that return 0 is a valid implementation of hashCode().

You should really be writing a pair or a tuple class instead of using arrays or manually putting in hash codes that you should not assume are unique.

Sign up to request clarification or add additional context in comments.

5 Comments

My problem is that I want a fast lookup of weight given tuple, i.e Mapping of (x, y) -> w. How can I do that without hashmaps?
I didn't say don't use hash maps. I said to write a pair or tuple type to use as the key in the hash map.
I think I dont follow very well. If I use my own data type like pair or tuple, I still need to compute a unique hashcode right?
No, you need to compute a half-decent hash code. As long as you write a correct equals implementation, however, your map will work correctly.
Thanks for sending me in the right direction. Using a Pair class helped me optimize the code a bit more as well.
0

Using arrays as keys in a map is a bad idea, as their hashCode implementation is not based on their contents. You should instead use a collection in place of your array(s), or a class that wraps your array and properly implements equals and hashCode.

1 Comment

I am using deepHashCode() which should be based on contents

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.