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

ObjectHashSet does not implement equals in a compatible fashion to HashSet/AbstractSet #120

Closed
nitsanw opened this issue Dec 15, 2017 · 4 comments
Labels

Comments

@nitsanw
Copy link
Contributor

nitsanw commented Dec 15, 2017

In essence, it does not accommodate other implementations of the Set interface, such that a HashSet and an ObjectHashSet of the same contents are not equal. This blocks the adoption of ObjectHashSet as a drop in replacement. Happy to provide a PR if we agree this is desirable to be fixed.

@nitsanw nitsanw added the bug label Dec 15, 2017
@mjpt777
Copy link
Contributor

mjpt777 commented Dec 15, 2017

Go for it.

@nitsanw
Copy link
Contributor Author

nitsanw commented Dec 15, 2017

Further notes from Collection::equals:
The contracts for List.equals and Set.equals state that lists are only equal to other lists, and sets to other sets. Thus, a custom equals method for a collection class that implements neither the List nor Set interface must return false when this collection is compared to any list or set. (By the same logic, it is not possible to write a class that correctly implements both the Set and List interfaces.)"
From the Set::equals javadoc:
"Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface."

@mjpt777
Copy link
Contributor

mjpt777 commented Dec 16, 2017

@RichardWarburton and I spent some time on the map implementations so they work for equals and hashCode but the set implementations have not had the same love.

@RichardWarburton
Copy link
Contributor

Indeed, special attention needs to be taken to the .hashCode() method as collections actually specify the algorithm you should use for calculating it.

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

No branches or pull requests

3 participants