WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Expert · question 61 of 100

How does the hashCode() method work in Java Collections, and why is it important?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

The hashCode() method is a method defined in the Object class of Java that is used to generate a hash code value for an object. In the context of Java Collections, the hashCode() method is used to determine the hash code value for keys in hash-based data structures such as HashMap, HashSet, and Hashtable.

The hashCode() method generates a 32-bit integer value based on the contents of the object. If two objects are equal according to the equals() method, then they must have the same hash code value. However, the reverse is not necessarily true: if two objects have the same hash code value, they are not necessarily equal according to the equals() method. This is known as a hash collision.

The hash code value is used as an index into the hash table or bucket array, which is an array of linked lists. The objects that have the same hash code value are stored in the same linked list. When searching for an object in a hash-based data structure, the hash code value is used to determine which linked list to search in, which can greatly reduce the search time.

It is important to implement the hashCode() method correctly when creating custom objects that will be used as keys in hash-based data structures. If two objects that are equal according to the equals() method do not have the same hash code value, then they may not be found in the same linked list and the hash-based data structure will not work correctly.

Here is an example of how to implement the hashCode() method for a custom object:

public class Person {
    private String firstName;
    private String lastName;
    private int age;

    public Person(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    // Getters and setters

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof Person)) {
            return false;
        }
        Person other = (Person) obj;
        return firstName.equals(other.firstName) \&\& lastName.equals(other.lastName) \&\& age == other.age;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + firstName.hashCode();
        result = 31 * result + lastName.hashCode();
        result = 31 * result + age;
        return result;
    }
}

In this example, the hashCode() method is implemented using the formula 31 * hash + field.hashCode() for each field in the object. The constants 17 and 31 are arbitrary prime numbers that are used to initialize and update the hash code value. The formula ensures that small changes in the object will result in large changes to the hash code value, which reduces the likelihood of hash collisions.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics