HashMaps comparison always seems to return the same hash code
See original GitHub issueHi,
Given the following
describe("HashMap", () => {
it("Gives unique hash codes", () => {
const map1 = HashMap.of(["48", 1], ["49", 2], ["50", 3], ["51", 4]);
const map2 = HashMap.of(["49", 2], ["50", 3], ["51", 4]);
const map3 = HashMap.of(["49345678", 2], ["50", 3], ["51", 4]);
console.log(map1.hashCode(), map2.hashCode(), map3.hashCode());
expect(map2.hashCode() !== map1.hashCode()).toBeTruthy();
expect(map3.hashCode() !== map1.hashCode()).toBeTruthy();
expect(map3.hashCode() !== map2.hashCode()).toBeTruthy();
});
});
All hash maps are giving me the exact same hash code:
● HashMap › Gives unique hash codes
expect(received).toBeTruthy()
Received: false
31 | console.log(map1.hashCode(), map2.hashCode(), map3.hashCode());
32 |
> 33 | expect(map2.hashCode() !== map1.hashCode()).toBeTruthy();
| ^
34 | expect(map3.hashCode() !== map1.hashCode()).toBeTruthy();
35 | expect(map3.hashCode() !== map2.hashCode()).toBeTruthy();
36 | });
at Object.<anonymous> (src/store/domain/types/DataClass.test.ts:33:49)
console.log src/store/domain/types/DataClass.test.ts:31
1696 1696 1696
Test Suites: 1 failed, 1 total
I’d expect some conflicts to be possible given the [comment] above the hashcode function, but since all 3 are returning 1696
even after arbitrary changes, this means we basically can’t compare hash maps.
This is on the latest version of PreludeTS.
I’d love to help to fix this but the logic behind generating the hashcode is a bit above my head right now ):
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
How does a Java HashMap handle different objects with the ...
If two keys are the same ( equals() returns true when you compare them), their hashCode() method must return the same number. If...
Read more >The 3 things you should know about hashCode()
Why? An object's hashCode method must take the same fields into account as its equals method. By overriding the equals method, you're declaring ......
Read more >Learning Java. equals() and hashCode() Methods
1) If two objects are equal (i.e. the equals() method returns true), they must have the same hashcode. 2) If the hashCode() method...
Read more >How HashMap works in Java - Javarevisited
Since each node contains an entry, HashMap keeps comparing the entry's key object with the passed key using equals() and when it returns...
Read more >Things every engineer should know: Hashmaps - Medium
A hash function can return a hash that has a value anywhere between the max negative and positive values for an integer.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ahh, that is such a silly mistake, I looked at this code trying to figure out what was going wrong and also completely missed it. I love it!
Thanks for the swift response/fix!!
Hi, I pushed prelude-ts 1.0.1 to npm with the fix. Right now it’s not visible on the npm website yet, but it’s probably just a matter of a cache refresh, the release should be there.