Home > Java > javaTutorial > The inner workings of HashMap in Java

The inner workings of HashMap in Java

WBOY
Release: 2023-08-28 13:37:46
forward
1319 people have browsed it

The inner workings of HashMap in Java

The function ‘hashCode’ is used to get the hash code of an object in Java. This is an object of the superclass Object. It returns the memory referenced by the object as an integer. It is a native function, which means that methods cannot be used directly in Java to obtain a reference to an object.

In order to improve the performance of HashMap, please use hashCode() correctly. Basically, this function is used to calculate bucket and index values. The way it is defined is as follows -

public native hashCode()
Copy after login

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

Capacity = number of buckets * load factor
Copy after login

The function "equals" is used to check equality between two objects. It is also given by the superclass Object. This function can be overridden in a custom class by providing a custom implementation. This function returns true or false depending on whether the two objects in question are equal.

Generate index values ​​so that the size of the array is not very large, thereby avoiding outOfMemoryException. The formula to find array index is -

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

Let’s see an example -

Example

Live Demonstration

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")));
   }
}
Copy after login

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
Copy after login

The class named "hash_map" defines a string and a constructor. This is overridden by another function called "hashCode". Here, the hashmap key values ​​are converted to integers and the hash code is printed. Next, the "equals" function is overridden and checks if the key is equal to the key of the hashmap. Class Demo defines a main function in which a new instance of HashMap is created. Elements are added to this structure using the "put" function and printed on the console.

The above is the detailed content of The inner workings of HashMap in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template