Since the same bucket ( tab ) can contain elements that have different hashes due to the % tab.length operation. Hash checking first is probably some performance optimization to avoid calling equals() if the hashes are different.
To give this as an example: let's say you have two complex objects with the expensive equals() method. One object has a hash equal to 1, while the other object has a hash 32. If you put both objects in a hash table that has 31 buckets, they will fall into the same bucket ( tab ). When adding a second (another object), you must make sure that it is not already in the table. You can use equals() right away, but it can be slower. Instead, you first compare hashes, avoiding costly equals() if it is not necessary. In this example, the hashes are different (even though they are in the same bucket), so equals() not required.
source share