Table of Contents
Java HashMap:
- HashMap in java
- How HashMap works in java
- hash and indexfor method in HashMap
- hashcode and equals method in java
- How to sort HashMap by keys and values
- Difference between HashMap and HashSet
- Difference between HashMap and Hashtable
- How to iterate over HashMap
- hashcode()
- equals()
hashcode() and equals() method
hashcode():
equals():
Lets override default implemenation of hashcode() and equals():
1. Country.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; public class Country { String name; long population; public String getName() { return name; } public void setName(String name) { this.name = name; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; public class EqualityCheckMain { /** * @author arpit mandliya */ public static void main(String[] args) { Country india1=new Country(); india1.setName("India"); Country india2=new Country(); india2.setName("India"); System.out.println("Is india1 is equal to india2:" +india1.equals(india2)); } } |
1 2 3 |
Is india1 is equal to india2:false |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Country other = (Country) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } |
1 2 3 |
Is india1 is equal to india2:true |
One thing to remember here, signature of equals method should be same as above.
Lets put this Country objects in hashmap:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.java2blog; import java.util.HashMap; import java.util.Iterator; public class HashMapEqualityCheckMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { HashMap<Country,String> countryCapitalMap=new HashMap<Country,String>(); Country india1=new Country(); india1.setName("India"); Country india2=new Country(); india2.setName("India"); countryCapitalMap.put(india1, "Delhi"); countryCapitalMap.put(india2, "Delhi"); Iterator countryCapitalIter=countryCapitalMap.keySet().iterator(); while(countryCapitalIter.hasNext()) { Country countryObj=countryCapitalIter.next(); String capital=countryCapitalMap.get(countryObj); System.out.println("Capital of "+ countryObj.getName()+"----"+capital); } } } |
1 2 3 4 |
Capital of India----Delhi Capital of India----Delhi |
1 2 3 4 5 6 7 8 9 |
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } |
1 2 3 |
Capital of India----Delhi |
This is the reason java doc says “if you override equals() method then you must override hashCode() method”
hashcode() and equals() contracts:
equals():
The equals method implements an equivalence relation on non-null object references:
- It is reflexive: for any non-null reference value x, x.equals(x) should return true.
- It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
- It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
- It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
- For any non-null reference value x, x.equals(null) should return false.
hashcode():
- Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
- If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
- It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Key points to remember:
- If you are overriding equals method then you should override hashcode() also.
- If two objects are equal then they must have same hashcode.
- If two objects have same hashcode then they may or may not be equal
- Always use same attributes to generate equals and hashcode as in our case we have used name.