
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
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.
- Related Articles
- Internal working of Set/HashSet in Java
- Internal working of Python
- Internal working of Set in Python
- Internal working of the list in Python
- The internal working of the ‘foreach’ loop in PHP
- HashMap in Java
- Clone HashMap in Java
- Initialize HashMap in Java
- Displaying content of a HashMap in Java
- Create a HashMap in Java
- Display HashMap elements in Java
- Hashmap vs WeakHashMap in Java
- Find the size of a HashMap in Java
- Iterate through the values of HashMap in Java
- Get the count of elements in HashMap in Java
