Java - Compare the Objects using equals() & hashCode()

preview_player
Показать описание
- By default equals method only compare the reference object
- To compare based on Object value, override the equals & hashcode method
- First compare by hashcode(), if hashCode of both Objects are different, then objects are not equal
- but if hashcode of both Objects are same, then we check the equals method
- hashcode is used to quickly checl the equality

- Now whenever you're adding Object in Set or Map, then also you need to overide the equals & hashcode
- Because, Set & Map check the equality & if Object are equal it won't add again so you need to add equality

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 400+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

Great explanation, I was scratching my head from last 2 hours and your 16 min video made it clear easier than books and Udemy courses. Keep it up, man.

samirpanchal
Автор

sir, you stop making videos ?? your video is very good for fresher as well as experienced people especially those who want to learn Dynamic programming what great content you have sir. And the very important thing which I want to thank you that you have created these videos 2 years before only when no one was giving this imp concept for free and you gave.

Thank you sir

techieadi
Автор

Great video, really helped me grasp the concepts. I have question, is it a good idea to put the hashCode equality check inside of the equals method?

iamshr
Автор

Thank you so much for the tutorial it was really well explained, although i wish you explained a little bit more the casting part " Employee emp = (Employee) obj ", but other than that great video !

larbiaitdjebara
Автор

God level after watching it I have one doubt if e1 and e2 have same content and we are overriding only equals method then it will never calls equals method bcoz their hashcode is different according to the object class hashcode method.then it will add e1 and e2 in hashset even though their content is reply

adityalodhi
Автор

Thank you for knowledge
Very useful
I have one dout
What happens if we override only equals method not overriding hash code
In case map is it will consider
Both objects are equal or not

Should we override both methods or is it mandatory to check for equality


Please reply
Thanks advance

e.ch.vidyasagarkorada
Автор

Thank you very much dude. Your explanation is so neat and objective. Kudos on you

nurbekhalikulov
Автор

thankyou you cleared all my doubts in this concept,
thanks a lot :))

sujithkumar
Автор

great explanation..Simple and not at all confusing

shynimol
Автор

Great video. Alternatively if you are comparing JSON then DeltaJSON is a really useful set of tools for compare, merge and graft.

markthomas
Автор

Very well explained. spot on explanation. Thank you so much for this video

namrathas
Автор

Very nice explanation actually. 16 mins worth it. Thank you lot

abirkec
Автор

== compares reference and .equals compares the values is what I learnt

naveenprasanth
Автор

Sir please upload more videos on Strings, Graphs and Dynamic Programming.

Thanks so much in advance.

abhinavminocha
Автор

thanks a lot dude
it's just what I was looking for :)

academico
Автор

great explanation sir Thank u so much sir

ANJALISingh-cqnt
Автор

To override the hashcode is that the only way or do we have any other way to check the hashcode values of two objects?

SHAnkarKumar-goxk
Автор

Nice explanation
To the point
Exactly what I was looking for

shubhamgagre
Автор

do you mean equals() and hashcode() exists only for set and map but not for list?

harikagoud
Автор

beautifully explained..thank you so much:)

brandyplays