question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HashMaps comparison always seems to return the same hash code

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Nov 26, 2020

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!!

0reactions
emmanueltouzerycommented, Nov 25, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found