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

Показать описание
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
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
Комментарии