JAVA Collection Interview QNA - How Hashmap works internally? | hashcode() , equals() & THE Contract

preview_player
Показать описание
Few of the popular JAVA Collection Interview questions comes from the Hash based collections. The HashSet, Hashmap and the method like hashcode() , equals() & THE Contract between the hashcode() and equals() are the most popular one.

In this video, we are going to discuss regarding the above questions and we will add another important JAVA Collection Interview QNA to our “ java interview questions and answers “ playlist.

We will start this session with some hashset related questions and the internal working of hashset, and these questions has a direct relation with the topic that we will be covering in this session below.

TIMESTAMP
—————

00:00:00 - Introduction - JAVA Collection Interview QNA
00:02:46 - Hashset In Java - Odd behaviour
00:14:47 - Java equals() - Deep Dive
00:31:11 - Java hashcode() - Deep Dive
00:56:27 - How Hashmap works internally?
01:16:59 - Hashmap internals - Debugging
01:39:50 - hashcode() , equals() & THE Contract
01:46:40 - Integer Caching - Integer as a Hashmap key
01:48:46 - Scenario 1 - No hashcode() and equals() overridden
02:02:49 - Scenario 2 - Override hashcode() but not equals()
02:11:19 - Scenario 3 - Override equals() but not hashcode()

When we use custom objects as a Hashmap key, we should consider overriding hashcode() and equals() methods.
Incase the hashcode() and the equals() are not overridden, the object class hashcode and equals participate during the object insertion and,
causes in duplicate insertions to the map.

The hashcode() methods determines the hash of an object which later used for index calculation.

The Index is a smaller number which is considered as the bucket number where an Hashmap
Entry will go.

As the index is calculated from a hashcode it is very important that, the index should be same for duplicate objects. By doing so we can send all duplicate objects into a single bucket resulting the equals checks to trigger. The equals method decides whether the objects are equals and stop duplicate entries to be inserted to the map. So we should override the equals method If the component is getting used as a key inside a Hashmap.

We should give implementation to equals method in such a way that it can determine the object equality (may be based on the object reference / content).

We should follow the below two golden rules while we are writing our custom equals and hashcode methods
1. Two equal object should have the same hashcode.
2. If the hashcode of the two objects are same, that don’t define the object equality.

Let’s go ahead and have a deep dive into hashcode , equals , hashset, hashmap, and the internal working of hashset and hashmap.
Hope this video will help for your java interview preparation.

#HashmapInternal #Hashmap #JavaCollectionFramework #Java

For More free/paid courses visit

LINKS AND FREE LEARNING RESOURCES
========
Watch my new mock interview series for java developers

Watch Spring boot Hot topics

Java Interview / Question and Answer series

Explain spring Bean Lifecycle

spring core

spring mvc

spring mvc intermediate

Spring JDBC

spring live project

SPRING SECURITY COURSE[NEW]

For more courses visit

Any time you are getting stuck with issues, Feel free to ask for support.

you can ping me on my Facebook page

Make sure to join my private Facebook page (Ask me here)
“SeleniumExpress - Support"

you can ask for support in my website forum

Subscribe to my youtube channel

Follow me on Insta
Рекомендации по теме
Комментарии
Автор

Video was long but much better than watching a bollywood movie....learned alot from scratch. Thank you so much sir but i wanted to say that you are a very underrated teacher.

chintu
Автор

the explanation is very understandable and clear. I wish we have a chance to learn Threads related questions' answers from you

fatmao
Автор

At 27:9 m, Integer i1=100 and Integer i2=100 both i1 and i2 refer to the same object in the caches pool for Integer wrapper class so if you use == keyword that time it returns true.

ManishYadav-jxmb
Автор

-128 to 127 is the default size. But javadoc also says that the size of the Integer cache may be controlled by the -XX:AutoBoxCacheMax=<size> option

ankurpachegaokar
Автор

I am a lucky person I got this channel for spring

nitinchoudhary
Автор

Can you please make video on spring cloud

aayush
Автор

Superbb Explanation... Got a clear picture... Keep doing more like this... Will helpful alot

knizam
Автор

Great content! Good learning resources for all levels of developers

chukwukaegbujio
Автор

I like you are adding chapters time slot wise thanks

MHK
Автор

I already like deep dive videos from you ❤

nguyenquan
Автор

hi abilsh the way you teach its brilliant but i have some questions how to know how many dispatcher servlet is enough for the application and also how to create multiple dispatcher servlet in spring boot and how to install spring boot app in company server can you please make videos and let me know thanks.

teklehaimanotatikilt
Автор

Hello
I really appreciate your work but you still go from course to another course
when will you finish SOLID course ?
I love your teaching and please do not get me wrong .❤

adelhatary
Автор

your videos are amazing. one suggestion please don't add the background music.

uniqName
Автор

HI, Abhilash could u make video on Auth2

AbuSufiyan-mq
Автор

from scratch thats the uniqueness of ur channel ....love this channel

arjunvetal
Автор

Please Abhilash can you make a same video about threads?

aibardulatov
Автор

Please please post more Java interview questions and deep dive

suseesenthil
welcome to shbcf.ru