Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved LatLng.hashCode #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

ristiisa
Copy link

@ristiisa ristiisa commented Aug 2, 2024

Description:

This PR updates the hashCode implementation for the LatLng class to use the XOR strategy. This approach aims to reduce hash code collisions by providing a better distribution of hash codes compared to simple addition.

Changes:

Updated the hashCode method in the LatLng class to use XOR (latitude.hashCode ^ longitude.hashCode).

Benefits:

Reduced hash code collisions, leading to more efficient use of hash maps and sets.
Improved overall performance in collections that rely on hash codes.

Collision Example:

With the previous implementation using addition, the following pairs of coordinates produce the same hash code:

(latitude: 1.0, longitude: 2.0)
(latitude: 2.0, longitude: 1.0)
Both pairs will produce the same hash code since 1.hashCode + 2.hashCode == 2.hashCode + 1.hashCode.

By using the XOR strategy, these pairs will generate different hash codes, reducing the likelihood of collisions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant