I’m mapping x,y values onto a Cartesian plane with a HashMap. What would be an effective HashCode for very small x, very large y values?
currently I am using :
public int hashCode() {
return ((y * 31) ^ x);
// & Typical x,y values would be, (with many collisions on x):
[4, 1000001] [9, 1000000] [5, 999996] [6, 999995] [4, 999997]
[6, 999997] [6, 1000003] [10, 999994] [8, 999997] [10, 999997]
[5, 999999] [4, 999998] [5, 1000003] [2, 1000005] [3, 1000004]
[6, 1000000] [3, 1000005]
I am inserting both x,y pairs into the key of a hashmap with a .put method, to avoid any duplicate x,y pairs. Not sure if that is the most effective solution either.
Sometimes the best way to know is to just run some brute force tests on your ranges. Ultimately though, you can always write a hash function and go back and fix it later if your getting poor performance. Premature optimization is evil. Still, it’s easy to test hashing.
I ran this program and got 0 collisions:
And the output:
Note that my function was
7 + y * 31 + x * 23.Of course, don’t take my word for it. Mess with the ranges to tweak it to your data set and try calculating it yourself.
Using your
(y * 31) ^ xgave me:And using just
x * y:Be warned that this program can use a pretty good chunk of memory and computing power. I ran it on a pretty powerful server. I have no idea how it’ll run on a local machine.
Some good rules to follow for hashing are:
x * yin this test, I had a very large number of collisions.