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

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

16.455 Lượt nghe
JAVA Collection Interview QNA - How Hashmap works internally? | hashcode() , equals() & THE Contract
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 https://youtu.be/yX2w-Sof95s Watch Spring boot Hot topics https://youtu.be/CUQWZHjuq-w Java Interview / Question and Answer series https://youtu.be/Y1uGGTrBYKw Explain spring Bean Lifecycle https://youtu.be/hUYGGqoXEPc spring core https://www.youtube.com/watch?v=r2Q0Jzl2qMQ&list=PL3NrzZBjk6m-nYX072dSaGfyCJ59Q5TEi spring mvc https://www.youtube.com/watch?v=zCHrZdJODg4&list=PL3NrzZBjk6m-OSeL3DRMD_ISa0u-9c1l1 spring mvc intermediate https://www.youtube.com/watch?v=40YaLHbrwrY&list=PL3NrzZBjk6m_2jw8sC0BkEwSPFHxgQDWr Spring JDBC https://www.youtube.com/watch?v=5j_vh1b9dV8&list=PL3NrzZBjk6m-rYTKze-5Y5RvU8eykal4j spring live project https://www.youtube.com/watch?v=m_b_FwDjAXM&list=PL3NrzZBjk6m-9CVPueDLLermojwtfuW_f SPRING SECURITY COURSE[NEW] https://youtu.be/NMNLozLGT1s For more courses visit https://www.seleniumexpress.com Any time you are getting stuck with issues, Feel free to ask for support. You can send mail to [email protected] you can ping me on my Facebook page https://www.facebook.com/seleniumexpress Make sure to join my private Facebook page (Ask me here) “SeleniumExpress - Support" https://www.facebook.com/groups/187000222361579/ you can ask for support in my website forum https://www.seleniumexpress.com Subscribe to my youtube channel https://www.youtube.com/channel/UCpUYkSDJpHug-vfC5S-NXRA?sub_confirmation=1 Follow me on Insta https://www.instagram.com/selenium_express/