KEMBAR78
Java equals hashCode Contract | PPTX
Java Equals HashCode Contract
Sujit Kumar
Zenolocity LLC © 2013-2023
The Contract
• 2 equal objects should have the same
hashCode.
• The reverse may or may not be true => 2
objects with the same hashCode may or may
not be equal.
Default implementations in Object
class
• The default implementation of hashCode() in
Object class returns distinct integers for
different objects.
• The default implementation of equals() in
Object class is reference equality, and
therefore two instances are the same if and
only if they are the same instance.
Class with equals() but no hashCode()
public class Banana {
private String color;
public Banana (String color) {
this.color = color;
}
public boolean equals(Object obj) {
if (!(obj instanceof Banana)) return false;
if (obj == this) return true;
return this.color.equals(((Banana) obj).color);
}
}
equals() provided without hashCode()
Banana a1 = new Banana("green");
Banana a2 = new Banana(“yellow”);
//hashMap stores Banana type and its quantity
HashMap<Banana, Integer> m = new HashMap<Banana,
Integer>();
m.put(a1, 10);
m.put(a2, 20);
// Returns null even though the map contains a green Banana
System.out.println(m.get(new Banana("green")));
Add hashCode() method to Class
public int hashCode() {
return 29 * this.color.hashCode();
}
// Returns the green Banana now !
System.out.println(m.get(new Banana("green")));
Hash Collisions
• put method – calculates the hashCode on the key and
places the key-value pair in a bucket for that hashCode
value.
• The next put may result in the same hashCode if the
hashCode method is not good enough to produce a unique
value. This results in a hash collision.
• Hash Collision results in multiple entries (k-v pairs) put in
the same bucket in the form of a list.
• When you retrieve using the get method, calculate the
hashCode, go to the bucket & retrieve from the list in the
bucket by invoking the equals method for each key in the
list.
• Hash Collisions degrade performance.
Techniques to make a hashCode
unique
• Invoke the hashCode on each instance
variable, multiply with a prime number like 29
and keep adding the results.
• Return the final result.

Java equals hashCode Contract

  • 1.
    Java Equals HashCodeContract Sujit Kumar Zenolocity LLC © 2013-2023
  • 2.
    The Contract • 2equal objects should have the same hashCode. • The reverse may or may not be true => 2 objects with the same hashCode may or may not be equal.
  • 3.
    Default implementations inObject class • The default implementation of hashCode() in Object class returns distinct integers for different objects. • The default implementation of equals() in Object class is reference equality, and therefore two instances are the same if and only if they are the same instance.
  • 4.
    Class with equals()but no hashCode() public class Banana { private String color; public Banana (String color) { this.color = color; } public boolean equals(Object obj) { if (!(obj instanceof Banana)) return false; if (obj == this) return true; return this.color.equals(((Banana) obj).color); } }
  • 5.
    equals() provided withouthashCode() Banana a1 = new Banana("green"); Banana a2 = new Banana(“yellow”); //hashMap stores Banana type and its quantity HashMap<Banana, Integer> m = new HashMap<Banana, Integer>(); m.put(a1, 10); m.put(a2, 20); // Returns null even though the map contains a green Banana System.out.println(m.get(new Banana("green")));
  • 6.
    Add hashCode() methodto Class public int hashCode() { return 29 * this.color.hashCode(); } // Returns the green Banana now ! System.out.println(m.get(new Banana("green")));
  • 7.
    Hash Collisions • putmethod – calculates the hashCode on the key and places the key-value pair in a bucket for that hashCode value. • The next put may result in the same hashCode if the hashCode method is not good enough to produce a unique value. This results in a hash collision. • Hash Collision results in multiple entries (k-v pairs) put in the same bucket in the form of a list. • When you retrieve using the get method, calculate the hashCode, go to the bucket & retrieve from the list in the bucket by invoking the equals method for each key in the list. • Hash Collisions degrade performance.
  • 8.
    Techniques to makea hashCode unique • Invoke the hashCode on each instance variable, multiply with a prime number like 29 and keep adding the results. • Return the final result.