Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

06_ HashMap & HashSet and how do they internally work_ What is a hashing function_ _ 800+ Big Data & Java Interview FAQs

The document provides an overview of how HashMap and HashSet work internally, explaining the role of hashing functions in storing and retrieving data. It details the process of storing key/value pairs, the significance of the hashCode() method, and the impact of poor hashCode implementations on performance. Additionally, it highlights the importance of setting initial capacities to minimize rehashing and explains how HashSet utilizes HashMap for storage.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

06_ HashMap & HashSet and how do they internally work_ What is a hashing function_ _ 800+ Big Data & Java Interview FAQs

The document provides an overview of how HashMap and HashSet work internally, explaining the role of hashing functions in storing and retrieving data. It details the process of storing key/value pairs, the significance of the hashCode() method, and the impact of poor hashCode implementations on performance. Additionally, it highlights the importance of setting initial capacities to minimize rehashing and explains how HashSet utilizes HashMap for storage.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

06/12/2024, 19:31 06: HashMap & HashSet and how do they internally work? What is a hashing function?

ashing function? | 800+ Big Data & Java Interview FAQs

800+ Q&As Menu Contact & reviews Register Login

800+ Big Data & Java Interview FAQs


It pays to prepare & choose from 2-6 offers. Read & Write more code to fast-track & go places with confidence.

Select A to Z of Java & Big Data Techs


search here … Go

Home Why?  Career  Membership 

Home › Java › 300+ Core Java Q&As › Core Java - Collection API › 06: HashMap & HashSet and how do they

internally work? What is a hashing function?

06: HashMap & HashSet and 300+ Java Interview


Q&As - Quick Prep FAQs

how do they internally work? 300+ Java Interview FAQs 📌 

What is a hashing function? 150+ Spring & Hibernate


FAQs
150+ Java Architect FAQs 📌


 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

Q2. What is the HashMap lookup time in Big O notation? FAQs 📌 

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 📌 

Q3 How does a HashMap internally store data?


A3. A backing array for the buckets and a linked list (until Java 8)
Key Areas & Companion
and a “binary tree” (Java 8 onwards) to store the values.. Techs FAQs

Backing Array with buckets: as shown below. 16+ Tech Key Areas FAQs 

10+ Companion Tech Q&As 

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

800+ Enterprise & Core


Java Q&As

300+ Core Java Q&As 

300+ Enterprise Java Q&As 

Java & Big Data Tutorials

Tutorials - Golang 

Tutorials - Big Data 

Tutorials - Enterprise Java 


hashCode() returns 1, 45, etc

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.

Java equals vs hashCode

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

This means the “hashcode = 123” gets stored in index 3 in the


backing array. So you get numbers between 0 and 9 for the capacity
of 10. Once the HashMap reaches its 75% of the capacity indicated
by the default hash factor of 0.75, the backing array’s capacity is
doubled and the rehashing takes place to reallocate the buckets for
the new capacity of 20.

1
2 hashCode() % capacity
3
4 123 % 20 = 3
5 456 % 20 = 16
6

Now the above modulus operator approach of rehashing has a flaw.


What if the hashCode is a negative number? You don’t want a
negative index. Hence, an improved hashing formula would be to
shift out the sign bit and then calculate the remainder with the
modulus (i.e. %) operator.

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

a). Protect against poorer hashes by only extracting the important


lower bits.

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

b). Determine the index based on the hashCode and capacity.

1
2 static int indexFor(int h, int length) {
3 return h & (length-1);
4 }
5

Actual name value pairs are stored as a key/value pair LinkedList

As shown in the diagram, the key/value pairs are stored as linked


list. It is important to understand that two different keys can result in
the same hashcode say 123, and get stored in the same bucket. For
example, “John, 01/01/1956” & “Peter, 01/01/1995” in the above
example. So, how do you retrieve just “John, 01/01/1956”? This is
where you key class’s equals() method gets invoked. It loops
through the items in the LinkedList bucket “123” to retrieve the key
“John, 01/01/1956” by using the equals() method to find a match.
This is why it is important to implement the hashCode() & equals()
method in your key class. If you are using an existing wrapper class
like Integer or the String as a key, it is already implemented. If you
write your own key class like “MyKey” with name and DOB as the
attributes as in “John, 01/01/1956”, you are responsible for properly
implementing these methods.

Q5. Why is it a good practice to appropriately set the initial capacity


of a HashMap?
A5. To minimize the occurrences of rehashing.

Q6. How does a HashSet internally store data?


A6. A HashSet internally uses a HashMap. It stores the element as
both key and value.

Q7. What is the effect of implementing a poor hashcode() for an


object?
A7. The invocation of hashCode() method should return different
values for different objects. If it returns same values for different
objects then this results in more key/value pairs getting stored
against the same bucket. This adversely impacts performance of
HashMaps & HashSets.

Relevant topics
1) 5 Java Object class methods interview questions & answers.

2) HashMap from scratch Java example


https://www.java-success.com/hashmap-and-how-it-works/ 4/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

3) Stack from scratch Java example

‹ 03: Java GC tuning for low latency applications Huffman coding in Java ›

About Latest Posts

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

How to take the road less travelled?


Practicing & brushing up a few Q&As each day on broad number of main stream categories can make a huge impact in 3-12
months.

"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.

If you are pressed for time, popular categories are pinned 📌


for you to prioritise based on the job description. Here are my top career
making know-hows & tips to open more doors as a Java/Big Data Engineer, Architect or Contractor.

1. Feeling stagnated?
2. How to earn more?
3. Freelancing Vs contracting?
4. Self-taught professional?
5. Job Interview Tips
6. Resume Writing Tips

Top 12 popular posts last 30 days


300+ Java Interview Q&As with code, scenarios & FAQs

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)

18 Java scenarios based interview Q&As for the experienced – Part 1


Let's look at scenarios or problem statements & how would you go about handling those scenarios in Java. These scenarios…
(1,541)

Membership Levels
Membership prices listed below are in Australian Dollars (A$). If you purchase in other currencies like Indian rupees, the equivalent…
(718)

30+ Java Code Review Checklist Items


This Java code review checklist is not only useful during code reviews, but also to answer an important Java job…
(524)

8 real life Java scenarios with Situation-Action-Result (i.e. SAR) technique


The SAR (Situation-Action-Result) technique is very useful to tackle open-ended questions like: 1. What were some of the challenges you…
(522)

01: 30+ Java architect interview questions & answers – Part 1


One of the very frequently asked open-ended interview questions for anyone experienced is: Can you describe the high-level architecture of…
(480)

15 Ice breaker interview Q&As asked 90% of the time


Most interviews start with these 15 open-ended questions. These are ice breaker interview questions with no right or wrong answers…
(412)

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)

02: 10 Java String class interview Q&As


Java Collection interview questions and answers and Java String class interview questions and answers are must know for any Java…
(357)

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

You might also like