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

Code Decode
Code Decode
10.9 هزار بار بازدید - 2 سال پیش - In this video of code
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:
https://openinapp.co/udemycourse

Course Description Video :
https://yt.openinapp.co/dmjvd

Equals and hash code explanation : Equals and Hashcode Contract in Java ...

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.
Entry.next points to next Entry in LinkedList.

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

If, hashCode() method is overridden properly, we will find bucket location using hashCode() method, we will obtain Entry on that bucket location, then iterate over each and every Entry (by calling Entry.next) and check whether new and existing keys are equal or not. If keys are equal replace key-value in Entry with new one, else call Entry.next But how to check whether two keys are equal or not. So, it’s time to implement equals() method.


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),
In this case, even if keys are equal, we will keep on calling Entry.next until we reach last Entry on that bucket location and ultimately we will end up storing new Entry (with new key) again in same bucket location.  tough it should be overridden.
Conclusion same key- value pair stored multiple times in HashMap.

when get() method is called-
we won’t be able to compare two keys (new key with existing Entry.key) and we will call Entry.next and again we won’t be able to compare two keys and ultimately when Entry.next is null - we will return false.
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?



Most Asked Core Java Interview Questions and Answers : Core Java frequently asked Interview ...

Advance Java Interview Questions and Answers : Advance Java Interview Questions

Java 8 Interview Questions and Answers : Java 8 Interview Questions(New Features)

Hibernate Interview Questions and Answers : Hibernate Interview Questions Java

Spring Boot Interview Questions and Answers : Advance Java Interview Questions

Angular Playlist :  Angular Course Introduction || Angular 8

SQL Playlist : SQL Interview Questions and Answers

GIT : GIT

Subscriber and Follow Code Decode
Subscriber Code Decode : https://www.seevid.ir/c/CodeDecode?...
LinkedIn : LinkedIn: codedecodeyoutube
Instagram : Instagram: codedecode25

#equalsandhashcode #codedecode #javainterviewquestions
2 سال پیش در تاریخ 1401/03/31 منتشر شده است.
10,961 بـار بازدید شده
... بیشتر