What about equals() and hashCode()? - Cracking the Java Coding Interview

preview_player
Показать описание
Cracking the #Java #Coding #Interview - Question 10: What about equals() and hashCode()?
Рекомендации по теме
Комментарии
Автор

please don't use the background music. also, please improve the audio quality as it's not easier to hear with full volume sometimes.

ankit-gupta
Автор

Yes background music is higher than the actual voice.

davidprasad
Автор

Or even better: Use records and have the JVM automatically provide appropriate equals() and hashCode() implementations.
Implementing hashCode() and equals() explicitly should probably be considered a code smell in Java 16+ (under most circumstances anyway).

jay_sensz
Автор

The typical hashCode() (* 31 +) implementation generates lots of clashes. This is true for hash code generated with your IDE or if you use a record. My advice is:

1. Whenever implementing hashCode() and equals(), also /always/ implement Comparable. This is your safety in case you have a poor hash code and end up with many keys in the same bucket. HashMap Java 8+ can then create a red-black tree for you. This is true whether you use Lombok, records, your IDE, or hand-write your hashCode() method.

2. Try and write as "perfect" a hashCode() method as possible. Take a moment to think about the possible values and how you can make them as distinct as you can. Spreading out the numbers is not that important.

3. Lastly, try and make the hashCode() as numerically small (above 0) as possible.

Do this, and you will typically get a factor 3x speedup over what the IDE and records generate.

heinzkabutz
Автор

Sir please don't use BGM..also improve the audio quality so that we can clearly grasp knowledge which is given by you..

umasingh
Автор

Why does using an IDE get a bulletpoint?

tylerbakeman
Автор

Tbh, these simple ones are never asked in interviews....

cccc
Автор

Please. Never ever use the background music again. And increase your voice volume. There must be some production person at Oracle who can do this for you.

redorange