Internal Working of HashMap in Java


The function ‘hashCode’ is used to get the hash code of an object in Java. This is an object of super class Object. It returns the object reference’s memory as an integer. It is a native function, which means no direct method in Java can be used to fetch the reference of the object.

For better performace of HashMap, use the hashCode() properly. Basically, this function is used to calculate the bucket and index values. It is defined in the following way −

public native hashCode()

Since we have mentioned ‘bucket’, it is important to understand what it means. It is an element that is used to store nodes. There can be more than two nodes in a single bucket. The nodes can be connected using linked list data structure. The capacity of the hashmap can be calculated using the bucket and load factor.

Capacity = number of buckets * load factor

The function ‘equals’ is used to check the equality between two objects. It is also given by super class Object. This function can be overridden in the customized class by providing customized implementation. This function returns true or false depending on whether the two objects in questions are equal or not.

An index value is generated so that the size of array is not big, thereby avoiding outOfMemoryException. The formula to find the index of the array is −

Index = hashCode(key) & (n-1) – Here n refers to number of buckets.

Let us see an example −

Example

 Live Demo

import java.util.HashMap;
class hash_map{
   String key;
   hash_map(String key){
      this.key = key;
   }
   @Override
   public int hashCode(){
      int hash = (int)key.charAt(0);
      System.out.println("The hash code for key : " + key + " = " + hash);
      return hash;
   }
   @Override
   public boolean equals(Object obj){
      return key.equals(((hash_map)obj).key);
   }
}
public class Demo{
   public static void main(String[] args){
      HashMap my_map = new HashMap();
      my_map.put(new hash_map("This"), 15);
      my_map.put(new hash_map("is"), 35);
      my_map.put(new hash_map("a"), 26);
      my_map.put(new hash_map("sample"), 45);
      System.out.println("The value for key 'this' is : " + my_map.get(new hash_map("This")));
      System.out.println("The value for key 'is' is: " + my_map.get(new hash_map("is")));
      System.out.println("The value for key 'a' is: " + my_map.get(new hash_map("a")));
      System.out.println("The value for key 'sample' is: " + my_map.get(new hash_map("sample")));
   }
}

Output

The hash code for key : This = 84
The hash code for key : is = 105
The hash code for key : a = 97
The hash code for key : sample = 115
The hash code for key : This = 84
The value for key 'this' is : 15
The hash code for key : is = 105
The value for key 'is' is: 35
The hash code for key : a = 97
The value for key 'a' is: 26
The hash code for key : sample = 115
The value for key 'sample' is: 45

A class named ‘hash_map’ defines a string and a constructor. This is overridden by another function named ‘hashCode’. Here, the key values of hashmap are converted to integer and the hash code is printed. Next, the ‘equals’ function is overridden and checks if the key is equal to the hashmap’s key. The class Demo defines a main function where a new instance of the HashMap is created. Elements are added into this structure using the ‘put’ function and printed on the console.

Updated on: 04-Jul-2020

989 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements