3

This question is asked by interviewer that most of answers related to hash code is used for bucketing where it checks equals to search objects.

Is there any other general use case or scenario, where hash code is beneficial and can be used in a routine program?

As recently I have used JPA where it throws exception "Composite-id class does not override hashCode()" but again it is used by implementation class of hibernate. Sly, what other places or scenario we can use hashcode other then collections especially scenario where you have used it yourself.

class a {
    public int hashCode() {
    }
}

class b {
    public static void main(String[] str) {
        //In what ways can i use hashcode here?
    }
}
fatherazrael
  • 5,511
  • 16
  • 71
  • 155

3 Answers3

2

Let's say your class will not be used in any collection ever( which is very unlikely though), it will be used in more than one place and by other developers. Any developer using your class will expect that if two instances of that class are equal based on equals method, they should produce same hashCode value. This fundamental assumption will be broken if hashCode is not overridden to be consistent with equals and that will prevent their code to function properly.

From Effective Java , 3rd Edition :

ITEM 11: ALWAYS OVERRIDE HASHCODE WHEN YOU OVERRIDE EQUALS

You must override hashCode in every class that overrides equals. If you fail to do so, your class will violate the general contract for hashCode, which will prevent it from functioning properly in collections such as HashMap and HashSet. Here is the contract, adapted from the Object specification :

• When the hashCode method is invoked on an object repeatedly during an execution of an application, it must consistently return the same value, provided no information used in equals comparisons is modified. This value need not remain consistent from one execution of an application to another.

• If two objects are equal according to the equals(Object) method, then calling hashCode on the two objects must produce the same integer result.

• If two objects are unequal according to the equals(Object) method, it is not required that calling hashCode on each of the objects must produce distinct results. However, the programmer should be aware that producing distinct results for unequal objects may improve the performance of hash tables.

The key provision that is violated when you fail to override hashCode is the second one: equal objects must have equal hash codes. Two distinct instances may be logically equal according to a class’s equals method, but to Object’s hashCode method, they’re just two objects with nothing much in common. Therefore, Object’s hashCode method returns two seemingly random numbers instead of two equal numbers as required by the contract.

1

A small semantic mistake in the interview question. Hash code is not used to check equality, it's used to detect inequality. If hash codes are different the objects are guaranteed to be inequal. If the codes are equal the objects may be equal and need to be checked with the equals-method.

That said, if the hash code is cached, it could be used to speed up the equals method.

Torben
  • 3,805
  • 26
  • 31
  • OP is asking a question that is not relates to your answer. What are the use cases of `hasCode` other than in collections framework. Your answer tells how hashCode can be used. A call to `equals()` does not depend on `hashCode()` in general (outside of set and map). – Prashant Dec 24 '19 at 06:31
  • @Prashant You're mistaken. Question was "Is there any _other_ general use case or scenario, where hash code is beneficial and can be used in a routine program?" – Torben Dec 24 '19 at 13:31
1
  • Suppose you only override equals but not hashCode
  • This means that hashCode is inherited from Object
  • Object.hashCode always tries to return different hash codes for different objects (regardless if they are equal or not)
  • This means that you may end up with different hash codes for two objects that you consider to be equal.
  • This in turn causes these two equal objects to end up in different buckets in hash based collections such as HashSet.
  • This causes such collections to break.

More reference: https://programming.guide/java/overriding-hashcode-and-equals.html

Nivin John
  • 173
  • 1
  • 11
  • 2
    The OP's question was where `hashCode` is used other than collections framework. – Prashant Dec 24 '19 at 06:27
  • this is a similar question: https://stackoverflow.com/questions/22646456/jpa-hibernate-composite-id-class-does-not-override-equals – Nivin John Dec 24 '19 at 06:30
  • 2
    You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections. When entities don't have a composite ID, they have a single one, of one of the basic supported types (Integer, Long, String, etc.), and those classes already have a well-defined equals() (and hashCode()) method. – Nivin John Dec 24 '19 at 06:33