What is the purpose of the hashCode() method

๐Ÿ’ก Concept: hashCode() Method

The hashCode() method returns an integer hash code value for an object used by hashing data structures like HashMap and HashSet.

๐Ÿ“˜ Quick Intro

hashCode() provides a numeric representation of an object for efficient lookup, insertion, and retrieval in hash-based collections.

๐Ÿง  Analogy

Think of hashCode() as a shortcut or index number that helps you quickly find a book in a library without scanning all shelves.

๐Ÿ”ง Technical Explanation

  • hashCode() returns an int representing the object's memory address or computed hash.
  • Equal objects must return the same hashCode.
  • Used by hash-based collections for efficient data storage and lookup.
  • Override hashCode() when equals() is overridden to maintain contract.
  • Helps in distributing objects uniformly in hash buckets.

๐ŸŽฏ Use Cases

  • โœ… HashMap, HashSet, and Hashtable internal operations rely on hashCode().
  • โœ… Efficient retrieval of objects in hash-based collections.
  • โœ… Prevents collisions and improves performance when properly implemented.

๐Ÿ’ป Example: Overriding hashCode()


public class Person {
    private String name;
    private int age;

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + (name == null ? 0 : name.hashCode());
        result = 31 * result + age;
        return result;
    }
}

โ“ Interview Q&A

Q1: What does hashCode() return?
A: An integer hash code of the object.

Q2: Why override hashCode()?
A: To maintain consistency with equals().

Q3: How is hashCode() used in collections?
A: For efficient object lookup in hash tables.

Q4: Can two different objects have same hashCode()?
A: Yes, but rare and called collision.

Q5: Is hashCode() unique?
A: Not guaranteed.

Q6: What is the contract between equals() and hashCode()?
A: Equal objects must have same hashCode.

Q7: Can hashCode() change during object lifetime?
A: Should not change if object is used in collections.

Q8: What happens if hashCode() is not overridden?
A: Default method returns memory address based hash.

Q9: How to implement good hashCode()?
A: Use prime numbers and immutable fields.

Q10: Does hashCode() affect performance?
A: Yes, good hashCode() improves collection speed.

๐Ÿ“ MCQs

Q1. What does hashCode() return?

  • String value
  • Integer hash code
  • Boolean value
  • Object reference

Q2. Why override hashCode()?

  • For performance
  • Consistency with equals()
  • For debugging
  • For serialization

Q3. How is hashCode() used in collections?

  • For logging
  • Efficient lookup
  • For encryption
  • For sorting

Q4. Can different objects have same hashCode()?

  • No
  • Yes
  • Sometimes
  • Never

Q5. Is hashCode() unique?

  • Yes
  • No
  • Sometimes
  • Always

Q6. What is contract between equals() and hashCode()?

  • No relation
  • Equal objects same hashCode
  • Opposite values
  • Depends on JVM

Q7. Can hashCode() change after creation?

  • Yes
  • No
  • Should not
  • Sometimes

Q8. What if hashCode() not overridden?

  • Error
  • Default memory-based
  • Random
  • Null

Q9. How to implement good hashCode()?

  • Prime numbers
  • Random values
  • Strings
  • Objects

Q10. Does hashCode() affect performance?

  • No
  • Yes
  • Maybe
  • Unknown

๐Ÿ’ก Bonus Insight

A well-implemented hashCode() is essential for the performance and correctness of hash-based collections in Java.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: