Equals and HashCode in Java Interview Questions and Answers | Equals hashcode contract |Code Decode

preview_player
Показать описание
In this video of code decode of Equals and hash code we have covered Interview questions asked around this topic for experienced and freshers both.
Udemy Course of Code Decode on Microservice k8s AWS CICD link:

Course Description Video :

Why do we need to override equals and hashcode method?

In hash table what is
Bucket - is ArrayList / Tree of Entry objects.
Entry - is LinkedList which contains information about key, value and next.

Why to override hashcode method?

It helps in finding bucket location, where entry(with key-value pair) will be stored .
Entry (of type LinkedList) is stored in bucket (ArrayList).

Why do we need to override equals and hashcode method?

Why to override equals method?

Once we have located bucket location in which our Entry (with key-value pair) will be stored, Equals method helps us in finding whether new and existing keys are equal or not.

What if we don’t override equals and hashcode method?

If, hashcode method is not overridden then for same key every time hashCode() method is called it might produce different hashcode,

when put() method is called-
There might be possibility that same Entry (with key-value pair) will get stored at multiple locations in bucket. And if key is same we might expect to override existing object which might not happen and same key might be present at 2 different locations.
Conclusion key- value pair may get stored multiple times in HashMap and may not be overridden if same key is added to hash table.

when get() method is called-
As there is possibility that hashCode() method might return different hashcode & rather than searching on bucket location where Entry(with key) exists we might be searching for key on some other bucket location.
Conclusion key exists in HashMap, but still we are not able to locate the bucket location in which it was stored.

If equals method is not overridden - though we will be able to find out correct bucket location if hashCode() method is overridden correctly, but still if equals method is not overridden, then

when put() method is called-
we might end up storing new Entry (with new key-value pair) multiple times on same bucket location (because of absence of equals method, we don’t have any way of comparing key’s),
Conclusion same key- value pair stored multiple times in HashMap.

when get() method is called-
Conclusion key existed in HashMap, but still we were not able to retrieve it.

So, it’s important to override equals method to check equality of two keys.

If two objects have same hashcode, are they always equal?

Subscriber and Follow Code Decode

#equalsandhashcode #codedecode #javainterviewquestions
Рекомендации по теме
Комментарии
Автор

Yes please create more video on this topic, it is so helpful.

bikashkonar
Автор

You are just savior for the developers like me who sticks to a company for a long time only because of not having regular touch with techinical skills. Mam, you are just awesome in explaining things more crisp and clearer.
You must deserve more and more subscribers and I just want you to thank in person as I watched most of your videos day and night that helps me to crack interviews in reputed organizations like harman etc.
Really loved your work to the core and no thank you will pay the value for your amazing work. WE ARE HERE TO SUPPORT YOU 🙌 KEEP GOING MAM🤩

pradeepn
Автор

Thanks, Code Decode Team for your comprehensive revision on core Java concepts. I was easily able to get 5 companies' Offer letters. Kindly keep up the good work.

kanishkarthirunavukkarasu
Автор

Thanks Code Decode team, please continue this series

kirankumar
Автор

Nice Explanation. I had asked by interviewer that, is Buckets gets created on hashmap instance creation time or any other time? I could not answer that time. When debugged put method of HashMap found that,
1.buckets created at the time of first put operation
2. as per your ppt presentation at 2:17 Bucket is not arrayList, its Array
/**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
transient Node<K, V>[] table;

pravinanandamahajan
Автор

Please create next video as soon as possible to cover rest of the questions. Thankful for the efforts taken. Your videos are key to crack any tech interview

gaurideshpande
Автор

Another good video, plz create 2nd part considering memory leak and covering more around question

nishantk
Автор

In the first slide shouldn't it be Node<K, V> instead of Entry ? 🤔

balakrushnabehera
Автор

Great 👍 please create second part of this video.

sourabhjinde
Автор

Excellent video once again
It's a request from your regular viewer please🙏 have a series on angular or atleast make us aware of some reference for learning angular and typescript because I'm facing a lot of difficulties in learning I have been assigned on a project on angular.
Please consider that🙏

shubhamkumar-gwvb
Автор

mam please upload second part of this series

amitjoshi