Interesting interview question on hashCode and equals method


One of the most interesting interview questions that I have encountered in my Java programming career is about the hashCode and equals methods. Interviewers always check whether the candidate knows equals() and hasCode() methods as they are the most important yet most confusing methods of the Java Object class. Both methods are used to check the equality of two or more objects. This article aims to provide some interesting interview questions related to hashCode() and equals() methods that will improve one's knowledge as well as skills.

Java Interview Questions on hashCode() and equals() method

When an interviewer starts questioning about hashCode() and equals() method, the first question might be do you know hashCode() and equals() method? So, let's start our discussion with this question.

hashCode() and equals() method in Java

The hashCode() method returns the value of hash code of objects. Hash code is an integer value that is generated for every object in Java. If we compare two objects, it will return the same hash value if both objects are equal as per equals() method otherwise returns a different hash value.

By default, equals() method only compares the memory location of objects, i.e., it checks whether the given objects are referring to the same object or not. But, we can override this method and put our own logic in it to compare values of the given object. It returns true if both the objects are the same otherwise false.

In which Package one can find the hashCode() and equals() method?

These methods are present in 'Object' class of 'java.util' package. The object class is the superclass of every class in Java.

List out the contract associated with hashCode() method

Here is the list of contracts associated with the hashCode() method −

  • If two objects are equal according to the equals method, then they must have the same hash code.

  • If two objects have the same hash code, they may or may not be equal according to the equals method.

  • The hash code of an object should not change during its lifetime unless the properties that affect its equality change.

List out the contract associated with equals() method

Here is the list of contract associated with the equals() method −

  • It is reflexive means for any non-null object obj, obj.equals(obj) should return true.

  • It is symmetric means for any non-null objects obj1 and obj2, obj1.equals(obj2) should return true if and only if obj2.equals(obj1) returns true.

  • It is transitive means for any non-null objects obj1, obj2, and obj3, if obj1.equals(obj2) returns true and obj2.equals(obj3) returns true, then obj1.equals(obj3) should return true.

  • For any non-null object obj, obj.equals(null) should return false.

Explain the importance of hashCode() and equals() method

The hashCode() and equals() methods are important because they define how an object behaves when it is stored in a collection that relies on hashing (used to map an object to an integer value), such as a HashMap or a HashSet. These collections use the hash code of an object to determine its slot in the underlying array, and then use the equals method to check for collisions or duplicates. If the hashCode and equals methods are not implemented correctly, the collection may not function properly and may produce unexpected results.

Is it necessary to override hashCode() and equals() methods together?

We should always override both methods together because they have a contract that needs to be maintained. If we only override one of them, we may break the contract and cause inconsistencies. The second thing is that we need to use the same set of properties or fields that determine the equality of an object in both methods. For example, if we use name and age in the equals method, we should also use them in the hashCode method.

Explain the term Collision of a Hash Collection

During the process of hashing, multiple objects might get the same integer value after getting evaluated by the in-built method 'equals()' which leads to a situation called collision. It occurs when two or more keys have the same hash value and are mapped to the same bucket resulting in slow performance.

What is the Difference Between "==" And equals() Method In Java?

Both '==' and equals() method are used to compare two entities and also, they check the memory location of objects while comparing. However, we can't override '==' operator and it is better to use this operator when we have to compare two primitive values.

Conclusion

The article showed the importance of equals() and hasCode() methods in Java interviews. We have discussed a list of important and interesting interview questions on hashCode() and equals() methods. One can easily crack the interview after understanding the discussed questions.

Updated on: 17-Aug-2023

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements