06_ HashMap & HashSet and how do they internally work_ What is a hashing function_ _ 800+ Big Data & Java Interview FAQs
06_ HashMap & HashSet and how do they internally work_ What is a hashing function_ _ 800+ Big Data & Java Interview FAQs
Home › Java › 300+ Core Java Q&As › Core Java - Collection API › 06: HashMap & HashSet and how do they
Posted on February 9, 2016 100+ Java code quality Q&As
150+ Java coding Q&As
HashMap & HashSet are not only one of the frequently used data
structures, but also one of the popular interview topics.
Q1. How does a HashMap store data? 300+ Big Data Interview
A1. As key/value pairs. You can store and retrieve values with the Q&As - Quick Prep FAQs
keys.
300+ Big Data Interview
A2. On average it is O(1) if the hashCode() method spreads out the 250+ Scala Interview FAQs
buckets as discussed below. A list has O(n) and a sorted list has 250+ Python interview
O(log n) to find an element. FAQs 📌
Backing Array with buckets: as shown below. 16+ Tech Key Areas FAQs
https://www.java-success.com/hashmap-and-how-it-works/ 1/7
06/12/2024, 19:31 06: HashMap & HashSet and how do they internally work? What is a hashing function? | 800+ Big Data & Java Interview FAQs
Tutorials - Golang
1) When you put an object into a map with a key and a value,
hashCode() method is implicitly invoked, and hash code value say
123 is returned. Two different keys can return the same hash value.
A good hashing algorithm spreads out the numbers. In the above
example, let’s assume that (“John”,01/01/1956) key and (“Peter”,
01/01/1995) key return the same hash value of 123.
2) Now when a hashCode value of say 123 is returned and the initial
HashMap capacity is say 10, how does it know which index of the
backing array to store it in? This is where the HashMap internally
invokes the hash(int ) & indexFor(int h, int length) methods. This is
known as the hashing function. This function in a very simple
explanation is like
https://www.java-success.com/hashmap-and-how-it-works/ 2/7
06/12/2024, 19:31 06: HashMap & HashSet and how do they internally work? What is a hashing function? | 800+ Big Data & Java Interview FAQs
1
2 hashCode() % capacity
3
4 123 % 10 = 3
5 456 % 10 = 6
6
1
2 hashCode() % capacity
3
4 123 % 20 = 3
5 456 % 20 = 16
6
1
2 (123 & 0x7FFFFFFF) % 20 = 3
3 (456 & 0x7FFFFFFF) % 20 = 16
4
This ensures that you get a positive index value. If you look at the
Java 8 HashMap source code, its implementation uses
1
2 static int hash(int h) {
3 // This function ensures that hashCodes that differ only
4 // constant multiples at each bit position have a bounded
5 // number of collisions (approximately 8 at default load
6 h ^= (h >>> 20) ^ (h >>> 12);
7 return h ^ (h >>> 7) ^ (h >>> 4);
8 }
9
https://www.java-success.com/hashmap-and-how-it-works/ 3/7
06/12/2024, 19:31 06: HashMap & HashSet and how do they internally work? What is a hashing function? | 800+ Big Data & Java Interview FAQs
1
2 static int indexFor(int h, int length) {
3 return h & (length-1);
4 }
5
Relevant topics
1) 5 Java Object class methods interview questions & answers.
‹ 03: Java GC tuning for low latency applications Huffman coding in Java ›
Arulkumaran Kumaraswamipillai
Mechanical Engineer to self-taught Java engineer in 1999. Contracting since 2002 as a Java Engineer &
Architect and since 2016 as a Big Data Engineer & Architect. Preparation & key know-hows empowered me
to attend 150+ job interviews, choose from 130+ job offers & negotiate better contract rates. Author of the
book "Java/J2EE job interview companion", which sold 35K+ copies & superseded by this site with 3.5K+
registered users Amazon.com profile | Reviews | LinkedIn | LinkedIn Group | YouTube Email: java-
interview@hotmail.com
"You are paid to read & write lots of code & solve business problems in a
collaborative environment"
100+ Free Java Interview FAQs
100+ Free Big Data Interview FAQs
Don't be overwhelmed by the number of Q&As. Job interviews are not technical contests to see who gets most number of questions right.
Nobody knows everything. The Clarity of the answers you give with real-life examples will go a long way in getting you multiple job offers.
It pays to brush-up & choose from 2-6 job offers. Experienced interviewers can easily judge your real experience from a few open-ended
questions & the answers you provide.
1. Feeling stagnated?
2. How to earn more?
3. Freelancing Vs contracting?
4. Self-taught professional?
5. Job Interview Tips
6. Resume Writing Tips
https://www.java-success.com/hashmap-and-how-it-works/ 5/7
06/12/2024, 19:31 06: HashMap & HashSet and how do they internally work? What is a hashing function? | 800+ Big Data & Java Interview FAQs
300+ Core Java Interview Q&As 01. Ice breaker Tell us about yourself? 02. Ice Breaker 8 Java real life scenarios…
(6,825)
300+ Big Data Interview Q&As with code, scenarios & FAQs
100+ SQL Interview Q&As 01. 50+ SQL scenarios based interview Q&As – What is wrong with this SQL code? 02.…
(2,425)
00: Top 50+ Core Java interview questions & answers for 1 to 3 years experience
Top 50 core Java interview questions covering core Java concepts with diagrams, code, examples, and scenarios. If you don't get…
(1,971)
Java 8 String streams and finding the first non repeated character with functional programming
Q1.Find the first non repeated character in a given string input using Java 8 or later? A1.Extends Find the first…
(1,669)
Membership Levels
Membership prices listed below are in Australian Dollars (A$). If you purchase in other currencies like Indian rupees, the equivalent…
(718)
0: 50+ SQL scenarios based interview Q&As – What is wrong with this SQL code?
This extends 18+ SQL best practices & optimisation interview Q&As. You can practice these SQLs by setting up the data…
(370)
https://www.java-success.com/hashmap-and-how-it-works/ 6/7
06/12/2024, 19:31 06: HashMap & HashSet and how do they internally work? What is a hashing function? | 800+ Big Data & Java Interview FAQs
Disclaimer
The contents in this Java-Success are copyrighted and from EmpoweringTech pty ltd. The EmpoweringTech pty ltd has the right to correct or enhance the current content without
any prior notice. These are general advice only, and one needs to take his/her own circumstances into consideration. The EmpoweringTech pty ltd will not be held liable for any
damages caused or alleged to be caused either directly or indirectly by these materials and resources. Any trademarked names or labels used in this blog remain the property of
their respective trademark owners. Links to external sites do not imply endorsement of the linked-to sites. Privacy Policy
© 2024 800+ Big Data & Java Interview FAQs Responsive WordPress Theme powered by CyberChimps
https://www.java-success.com/hashmap-and-how-it-works/ 7/7