SlideShare a Scribd company logo
4
Most read
9
Most read
15
Most read
Hashmap Internal Working
in Java
Introduction
HashMap is a part of Java’s collection since Java 1.2. It provides the basic
implementation of Map interface of Java. It stores the data in (Key, Value) pairs.
Equals and hashcode methods
In order to understand the internal working of HashMap, we must be aware of hashcode and equals method.
equals(Object otherObject) – As method name suggests, it is
used to simply verify the equality of two objects.
hashcode() – is a integer code generated for any variable/object
after applying a formula/algorithm on its properties. The hash code
for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1], where
s[i] is the ith character of the string, n is the length of the string.
What’s the output?
public static void main(String[] args) {
String s1 = "Compassites";
String s2 = "Software";
System.out.println( s1.equals(s2) );
System.out.println("Compassites " + s1.hashCode() );
System.out.println("Ea " + s2.hashCode() );
}
It Produces the following output.
false
Compassites hashCode: 1297712907
Software hasCode: 1383974343
What about this?
public static void main(String[] args) {
String s1 = "FB";
String s2 = "Ea";
System.out.println( s1.equals(s2) );
System.out.println("FB hashcode " + s1.hashCode() );
System.out.println("Ea hashcode " + s2.hashCode() );
}
What about this?
public static void main(String[] args) {
String s1 = "FB";
String s2 = "Ea";
System.out.println( s1.equals(s2) );
System.out.println("Compassites " + s1.hashCode() );
System.out.println("Ea " + s2.hashCode() );
}
It Produces the following output.
false
FB hashcode 2236
Ea hashcode 2236
You can see that the hashcodes are same for “FB” and “Ea”. So we can
conclude that...
Two Rules:
1. If two objects are equal then they must have the same
hashcode. (hascode of “aa” and “aa” are same)
2. If two objects have the same hashcode, they may or may not
be equal. (string “FB” has hashcode 2236 and “Ea” has
hashcode 2236 bu they are not equal)
To make it 2nd point clear, we can say that
“Birthday as HashCode”
Now that we are aware of Hashcode and Equals method, let’s
learn the internal working of Hashmap
Hashing?
HashMap is known as HashMap because it uses a technique
called Hashing.
Hashing is a technique of converting a large String to small
String that represents same String.
A shorter value helps in indexing and faster searches.
Internally HashMap contains an array of Node and a node is
represented as a class which contains 4 fields :
It can be seen that node is containing a
reference of its own object. So it’s a linked
list.
Hashmap:
Few Terms...
Buckets : A bucket is one element of HashMap array. It is used to store nodes.
Two or more nodes can have the same bucket. In that case link list structure is
used to connect the nodes.
Load Factor is a measure, which decides when exactly to increase the hashmap
capacity(buckets).
HashMap() : It is the default constructor which creates an instance of HashMap
with initial capacity 16 and load factor of 0.75. (meaning capacity is doubled
when 75% of hashmap is filled)
HashMap(int initialCapacity, float loadFactor) : It creates a HashMap instance
with specified initial capacity and specified load factor.
.
Put Operation in HashMap:
Say our program is something like this
HashMap<String, Integer> map = new HashMap<>();
scores.put (“Rohit”, 140);
scores.put (“Dinesh”, 70);
scores.put (“Dhoni”, 90);
scores.put (“Kholi”, 100);
scores.put (“Sachin”, 150);
scores.put (“Dravid”, 130);
Let’s see what is happening internally...
Initially Empty hashMap:
Here, the hashmap’s size is 16.(default hashmapsize)
HashMap map = new HashMap();
Inserting Key-Value Pair:
Let us understand at which location below key value pair will be saved into HashMap.
scores.put (“Rohit”, 140);
When you call the put function then it computes the hash code of the Key.
Lets suppose the Hash code of (“Rohit”) is 2657860.
Our array has an index till 15, so in order to store “Rohit”, we have to calculate index using a modular operation
Index = 2657860 % 16 => 4
Index = 4, So 4 is the computed bucket index value where the entry will sit as a
node in the HashMap.
How Hashmap works internally  in java
Hash Collision
Let us understand at which location below key value pair will be saved into HashMap.
scores.put (“Dhoni”, 90);
When you call the put function then it computes the hash code of the Key.
Lets suppose the Hash code of (“Dhoni”) is 63281940.
Our array has an index till 15, so in order to store “Rohit”, we have to calculate index using a
modular operation
Index = 63281940 % 16 => 4
How Hashmap works internally  in java
Get Operation in HashMap:
int rohitScore = scores.get (“Rohit”);
So get operation does the same as that of put operation. When the get function is
called it basically gets the hash code of the Key.
Lets suppose Hash of (“Rohit”) is 2657860
Index = 2657860 % 16 => 4
Now hashMap lookups at bucket index 4 for the hash code of the key
“2657860”.
Hash code “2657860” found then it lookup for the Key “Rohit” itself in
that node or linked list.
Thank you :)

More Related Content

DOCX
Angular Interview Questions & Answers
PPT
PPT
Angular 8
PPTX
Candlestick presentation
PPTX
Exception handling in Java
PDF
Using MLOps to Bring ML to Production/The Promise of MLOps
PPTX
G8-L1-WW1-ICT-as-a-Tool-in-Online-Marketing.pptx
PPTX
Apache Airflow overview
Angular Interview Questions & Answers
Angular 8
Candlestick presentation
Exception handling in Java
Using MLOps to Bring ML to Production/The Promise of MLOps
G8-L1-WW1-ICT-as-a-Tool-in-Online-Marketing.pptx
Apache Airflow overview

What's hot (20)

PPT
Java collection
PPT
Java Collections Framework
PDF
Java Collections Tutorials
PPT
Collection Framework in java
PDF
Collections Api - Java
PDF
Java Collection framework
PDF
Collections In Java
PPSX
Exception Handling
PPTX
Introduction to Spring Framework
PPTX
Event Handling in java
PPT
Jsp ppt
PPT
Exception Handling in JAVA
PPTX
Collections framework in java
PPTX
Java String
PPT
Spring Core
PPT
JDBC – Java Database Connectivity
PPSX
Spring - Part 1 - IoC, Di and Beans
PDF
Spring Framework - MVC
PDF
String handling(string class)
Java collection
Java Collections Framework
Java Collections Tutorials
Collection Framework in java
Collections Api - Java
Java Collection framework
Collections In Java
Exception Handling
Introduction to Spring Framework
Event Handling in java
Jsp ppt
Exception Handling in JAVA
Collections framework in java
Java String
Spring Core
JDBC – Java Database Connectivity
Spring - Part 1 - IoC, Di and Beans
Spring Framework - MVC
String handling(string class)
Ad

Similar to How Hashmap works internally in java (20)

PPTX
Presentation.pptx
PPTX
Hashing data
PPT
hashcode hash map hashing hashset hashing
PPSX
Data Structure and Algorithms: What is Hash Table ppt
PPTX
Data Structures : hashing (1)
PDF
Hash table
PPTX
IS_Hash Function fundamental concepts BS information technology .pptx
PPTX
Hashing.pptx
PPT
Chapter 10: hashing data structure
PPT
Hashing data structure in genaral ss.ppt
PPT
18 hashing
PPTX
hashing in data strutures advanced in languae java
PDF
Sienna 9 hashing
PPTX
Cryptography - Simplified - Hash Functions
PPTX
Hash table in java
PPTX
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
PDF
Lecture notesmap
PPTX
Hashing 1
PPTX
linear probing
PDF
Algorithms notes tutorials duniya
Presentation.pptx
Hashing data
hashcode hash map hashing hashset hashing
Data Structure and Algorithms: What is Hash Table ppt
Data Structures : hashing (1)
Hash table
IS_Hash Function fundamental concepts BS information technology .pptx
Hashing.pptx
Chapter 10: hashing data structure
Hashing data structure in genaral ss.ppt
18 hashing
hashing in data strutures advanced in languae java
Sienna 9 hashing
Cryptography - Simplified - Hash Functions
Hash table in java
unit-1-dsa-hashing-2022_compressed-1-converted.pptx
Lecture notesmap
Hashing 1
linear probing
Algorithms notes tutorials duniya
Ad

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Approach and Philosophy of On baking technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
A Presentation on Artificial Intelligence
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Programs and apps: productivity, graphics, security and other tools
Unlocking AI with Model Context Protocol (MCP)
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Building Integrated photovoltaic BIPV_UPV.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
A Presentation on Artificial Intelligence
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
Machine Learning_overview_presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Encapsulation theory and applications.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25-Week II
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Reach Out and Touch Someone: Haptics and Empathic Computing
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

How Hashmap works internally in java

  • 2. Introduction HashMap is a part of Java’s collection since Java 1.2. It provides the basic implementation of Map interface of Java. It stores the data in (Key, Value) pairs.
  • 3. Equals and hashcode methods In order to understand the internal working of HashMap, we must be aware of hashcode and equals method. equals(Object otherObject) – As method name suggests, it is used to simply verify the equality of two objects. hashcode() – is a integer code generated for any variable/object after applying a formula/algorithm on its properties. The hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1], where s[i] is the ith character of the string, n is the length of the string.
  • 4. What’s the output? public static void main(String[] args) { String s1 = "Compassites"; String s2 = "Software"; System.out.println( s1.equals(s2) ); System.out.println("Compassites " + s1.hashCode() ); System.out.println("Ea " + s2.hashCode() ); } It Produces the following output. false Compassites hashCode: 1297712907 Software hasCode: 1383974343
  • 5. What about this? public static void main(String[] args) { String s1 = "FB"; String s2 = "Ea"; System.out.println( s1.equals(s2) ); System.out.println("FB hashcode " + s1.hashCode() ); System.out.println("Ea hashcode " + s2.hashCode() ); }
  • 6. What about this? public static void main(String[] args) { String s1 = "FB"; String s2 = "Ea"; System.out.println( s1.equals(s2) ); System.out.println("Compassites " + s1.hashCode() ); System.out.println("Ea " + s2.hashCode() ); } It Produces the following output. false FB hashcode 2236 Ea hashcode 2236 You can see that the hashcodes are same for “FB” and “Ea”. So we can conclude that...
  • 7. Two Rules: 1. If two objects are equal then they must have the same hashcode. (hascode of “aa” and “aa” are same) 2. If two objects have the same hashcode, they may or may not be equal. (string “FB” has hashcode 2236 and “Ea” has hashcode 2236 bu they are not equal) To make it 2nd point clear, we can say that “Birthday as HashCode”
  • 8. Now that we are aware of Hashcode and Equals method, let’s learn the internal working of Hashmap
  • 9. Hashing? HashMap is known as HashMap because it uses a technique called Hashing. Hashing is a technique of converting a large String to small String that represents same String. A shorter value helps in indexing and faster searches.
  • 10. Internally HashMap contains an array of Node and a node is represented as a class which contains 4 fields : It can be seen that node is containing a reference of its own object. So it’s a linked list. Hashmap:
  • 11. Few Terms... Buckets : A bucket is one element of HashMap array. It is used to store nodes. Two or more nodes can have the same bucket. In that case link list structure is used to connect the nodes. Load Factor is a measure, which decides when exactly to increase the hashmap capacity(buckets). HashMap() : It is the default constructor which creates an instance of HashMap with initial capacity 16 and load factor of 0.75. (meaning capacity is doubled when 75% of hashmap is filled) HashMap(int initialCapacity, float loadFactor) : It creates a HashMap instance with specified initial capacity and specified load factor.
  • 12. .
  • 13. Put Operation in HashMap: Say our program is something like this HashMap<String, Integer> map = new HashMap<>(); scores.put (“Rohit”, 140); scores.put (“Dinesh”, 70); scores.put (“Dhoni”, 90); scores.put (“Kholi”, 100); scores.put (“Sachin”, 150); scores.put (“Dravid”, 130); Let’s see what is happening internally...
  • 14. Initially Empty hashMap: Here, the hashmap’s size is 16.(default hashmapsize) HashMap map = new HashMap();
  • 15. Inserting Key-Value Pair: Let us understand at which location below key value pair will be saved into HashMap. scores.put (“Rohit”, 140); When you call the put function then it computes the hash code of the Key. Lets suppose the Hash code of (“Rohit”) is 2657860. Our array has an index till 15, so in order to store “Rohit”, we have to calculate index using a modular operation Index = 2657860 % 16 => 4 Index = 4, So 4 is the computed bucket index value where the entry will sit as a node in the HashMap.
  • 17. Hash Collision Let us understand at which location below key value pair will be saved into HashMap. scores.put (“Dhoni”, 90); When you call the put function then it computes the hash code of the Key. Lets suppose the Hash code of (“Dhoni”) is 63281940. Our array has an index till 15, so in order to store “Rohit”, we have to calculate index using a modular operation Index = 63281940 % 16 => 4
  • 19. Get Operation in HashMap: int rohitScore = scores.get (“Rohit”); So get operation does the same as that of put operation. When the get function is called it basically gets the hash code of the Key. Lets suppose Hash of (“Rohit”) is 2657860 Index = 2657860 % 16 => 4 Now hashMap lookups at bucket index 4 for the hash code of the key “2657860”. Hash code “2657860” found then it lookup for the Key “Rohit” itself in that node or linked list.