In map the hash code is specified as the sum of the hashCodes of each entrance in the map's entrySet() view. The hashCode method returns the same int Object
hashCode() method of Map Interface in Java.
In this section we will discuss how can hashCode() method be implemented in Map interface in java.
Syntax
int hashCode()
This method gives a hash code value for the underlying map.
In map the hash code is specified as the sum of the hashCodes of each entrance in the map's entrySet() view. The hashCode method returns the same int Object ID if the two Objects are equal where equality is defined by the method equals( ) and the two different Object ID if they are not equal. i.e. if map1.equals(map2) is true then map1.hashCode( )= = map2.hashCode( ).
Parameter description
This method has no parameter but, it returns an integer value.
Example of hashCode() method
In this example we will show you how does hashCode() method work in Map interface. This example will help you to understand how you can found the hash code of a map. Further we will show you how does it generate the same object ID for the two different maps whose elements are same and the different ID when these map's elements are different.
Example :
package devmanuals.com; import java.util.Map; import java.util.HashMap; import java.util.TreeMap; public class MapHashCode { public static void main(String args[]) { Map mp1 = new HashMap(); mp1.put(1, "A"); mp1.put(2, "B"); mp1.put(3, "C"); mp1.put(4, "D"); System.out.println("Mapping1 = " + mp1); System.out.println("Size of map1 = " + mp1.size()); Map mp2 = new TreeMap(); mp2.put(1, "A"); mp2.put(2, "B"); mp2.put(3, "C"); mp2.put(4, "D"); System.out.println("Mapping2 = " + mp2); System.out.println("Size of map2 = " + mp2.size()); boolean bol = mp1.equals(mp2); System.out.println("Is the elements of map1 and map2 are equal : "+ bol); System.out.println("Hash code of map1 = " + mp1.hashCode()); System.out.println("Hash code of map2 = " + mp2.hashCode()); Map mp3 = new HashMap(); mp3.put(1, "E"); mp3.put(2, "F"); mp3.put(3, "G"); mp3.put(4, "H"); System.out.println("Mapping3 = " + mp3); System.out.println("Size of map3 = " + mp3.size()); System.out.println("Mapping1 = " + mp1); boolean bol1 = mp1.equals(mp3); System.out.println("Is the elements of map1 and map3 are equal : "+ bol1); System.out.println("Hash code of map1 = " + mp1.hashCode()); System.out.println("Hash code of map3 = " + mp3.hashCode()); System.out.println("Mapping2 = " + mp2); System.out.println("Mapping3 = " + mp3); boolean bol2 = mp2.equals(mp3); System.out.println("Is the elements of map2 and map3 are equal : "+ bol2); System.out.println("Hash code of map2 = " + mp2.hashCode()); System.out.println("Hash code of map3 = " + mp3.hashCode()); } }
Output :
Mapping1 = {1=A, 2=B, 3=C, 4=D} Size of map1 = 4 Mapping2 = {1=A, 2=B, 3=C, 4=D} Size of map2 = 4 Is the elements of map1 and map2 are equal : true Hash code of map1 = 256 Hash code of map2 = 256 Mapping3 = {1=E, 2=F, 3=G, 4=H} Size of map3 = 4 Mapping1 = {1=A, 2=B, 3=C, 4=D} Is the elements of map1 and map3 are equal : false Hash code of map1 = 256 Hash code of map3 = 280 Mapping2 = {1=A, 2=B, 3=C, 4=D} Mapping3 = {1=E, 2=F, 3=G, 4=H} Is the elements of map2 and map3 are equal : false Hash code of map2 = 256 Hash code of map3 = 280 |
[ 0 ] Comments