Рет қаралды 12,508
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
www.seleniumexpress.com
LINKS AND FREE LEARNING RESOURCES
========
Watch my new mock interview series for java developers
• Java interview questio...
Watch Spring boot Hot topics
• @Reposicotry Vs @Servi...
Java Interview / Question and Answer series
• Java collections inter...
Explain spring Bean Lifecycle
• REAL-TIME: Spring Bean...
spring core
• Spring framework tutor...
spring mvc
• Spring MVC course intr...
spring mvc intermediate
• [INTERMEDIATE] Spring ...
Spring JDBC
• Spring JDBC Course Pre...
spring live project
• #1 Build and Deploy A ...
SPRING SECURITY COURSE[NEW]
• Spring Security Course...
For more courses visit
www.seleniumexpress.com
Any time you are getting stuck with issues, Feel free to ask for support.
You can send mail to seleniumexpress@gmail.com
you can ping me on my Facebook page
/ seleniumexpress
Make sure to join my private Facebook page (Ask me here)
“SeleniumExpress - Support"
/ 187000222361579
you can ask for support in my website forum
www.seleniumexpress.com
Subscribe to my youtube channel
/ @seleniumexpress
Follow me on Insta
/ selenium_express