Question about contract between equals and hashCode is one of the most common question on interviews for Java developers. Let’s see how somebody should answer and what implications this contract has.
First look at this example:
This example can explain a lot. First of all string a and b are not equal, but their hashCode are the same.
That means that contract for equals and hashCode is:
- when two objects are equal, their hashCode must be the same,
- when two objects has the same hashCode, they don’t have to be equal.
Follow up questions
- Is hashCode implementation that returns constant value correct?
This is correct implementation but should be avoided because using such objects as keys in HashMap gives performance issues.
- How does HashMap handle objects with the same hashCode?
HashMap puts those keys to buckets and than it is using equals to find proper key.
Pingback: Maria Smith
Pingback: Cialis generic
Pingback: Viagra generic
Pingback: Viagra cialis levitra
Pingback: Viagra from canada
Pingback: Cialis generika
Pingback: Cialis online
Pingback: Viagra or viagra
Pingback: Cialis generico online
Pingback: Cialis online
Pingback: Cialis coupon
Pingback: writeaessay
Pingback: Cialis coupon
Pingback: Cialis 5 mg
Pingback: Buy cialis online
Pingback: Buy cialis online
Pingback: new siriustube692 abdu23na6290 abdu23na82
Pingback: sirius latest movs759 abdu23na6583 abdu23na21
Pingback: sfgjidfgn412 afeu23na1227 abdu23na17
Pingback: newtube tube planet950 afeu23na4033 abdu23na74
Pingback: tubela.net957 afeu23na647 abdu23na11
Pingback: 8558QyfD53l
Pingback: hdmobilesex.me
Pingback: Cialis 20mg
Pingback: cialis without a doctor's prescription canada
Pingback: cheap cialis without a doctors prescription
Pingback: online pharmacies canada
Pingback: prescriptions from canada without
Pingback: Viagra pills
Pingback: Viagra 5mg
Pingback: Viagra canada
Pingback: Viagra lowest price