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.

HashMap equality calculated incorrectly

See original GitHub issue

The following hashCodes should not be equal, but they are:

expect(HashMap.of(["a", 1], ["b", 0]).hashCode()).not.toBe(HashMap.of(["a", 0], ["b", 1]).hashCode());

The reason for this is that the HashMap hash code is calculated for the pairs in isolation, combining the hash codes of the kv-pairs and summing them up.

Calculating the pairs in isolation is correct because I think the order is not guaranteed in a HashMap (otherwise you could just send the whole list entries to fieldsToHashCode instead).

Adding up the hash code of the key and the value is not, however, because that produces the false match as demonstrated above, as adding doesn’t guarantee the uniqueness of the pairs of keys and values.

Something like this should fix it in the HashMap implementation:

HashMap.prototype.hashCode = function () {
  return this.hamt.fold(function (acc, value, key) {
    return acc + 37 * (Comparison_1.getHashCode(key) * Comparison_1.getHashCode(value));
  }, 0);
};

(Thank you for creating prelude-ts, by the way, our team has gotten a lot of value out of this library and I absolutely love it).

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
Annoiiedcommented, Nov 2, 2022

Hi again! I contributed a while ago but have a new github account, so if you see a ghost commit somewhere, that’s me

I wanted to see if I could get the tests to run with the given instructions, but with a fresh install of nodejs Gallium, simply running npm i fails for me as well 😃 could you maybe provide your lockfile, just to make sure its not an upstream package issue?

0reactions
emmanueltouzerycommented, Nov 5, 2022

published 1.0.5 on npm with the changes, that should be it! thank you again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

HashMap values().equals() is always return false even if ...
I am trying to check whether values of two hashmaps are equal to each other or not with using hashMap.values().equals().
Read more >
Internal Working of HashMap in Java - GeeksforGeeks
HashMap uses equals() to compare the key to whether they are equal or not. If the equals() method return true, they are equal...
Read more >
Things every engineer should know: Hashmaps - Medium
Through these steps we can take our key, and map it to a value. The constraint on a key is that you need...
Read more >
A Guide to Java HashMap - Baeldung
For this to work correctly, equal keys must have the same hash, however, different keys can have the same hash. If two different...
Read more >
Java equals() and hashCode() - DigitalOcean
If two objects are unequal according to equals() method, their hash code are not required to be different. Their hash code value may...
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