posted 12 years ago
The agreement is that objects which are equal have to provide the same hash code. Objects that have the same hash code, however, do not have to be equal.
You can store objects which have the same hash code in a HashSet or HashMap just fine. If they are equal, the second instance added will replace the first one. If the objects are not equal, you'll get what's called a hash collision. The map or set implementation will then take care of handling the second object appropriately, for instance by associating every used hashcode with a linked list holding all the non-equal objects with this hashcode.
You can run the following example yourself. Note that i have overridden hashCode in both Example classes to return a constant, something you obviously never want to do in the real world.