Joshua Bloch says on Effective Java

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, including HashMap, HashSet, and Hashtable.

Let's try to understand it with an example of what would happen if we override equals() without overriding hashCode() and attempt to use a Map.

Say we have a class like this and that two objects of MyClass are equal if their importantField is equal (with hashCode() and equals() generated by eclipse)

public class MyClass {
    private final String importantField;
    private final String anotherField;

    public MyClass(final String equalField, final String anotherField) {
        this.importantField = equalField;
        this.anotherField = anotherField;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((importantField == null) ? 0 : importantField.hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final MyClass other = (MyClass) obj;
        if (importantField == null) {
            if (other.importantField != null)
                return false;
        } else if (!importantField.equals(other.importantField))
            return false;
        return true;
    }
}

Imagine you have this

MyClass first = new MyClass("a","first");
MyClass second = new MyClass("a","second");

Override only equals

If only equals is overriden, then when you call myMap.put(first,someValue) first will hash to some bucket and when you call myMap.put(second,someOtherValue) it will hash to some other bucket (as they have a different hashCode). So, although they are equal, as they don't hash to the same bucket, the map can't realize it and both of them stay in the map.


Although it is not necessary to override equals() if we override hashCode(), let's see what would happen in this particular case where we know that two objects of MyClass are equal if their importantField is equal but we do not override equals().

Override only hashCode

If you only override hashCode then when you call myMap.put(first,someValue) it takes first, calculates its hashCode and stores it in a given bucket. Then when you call myMap.put(second,someOtherValue) it should replace first with second as per the Map Documentation because they are equal (according to the business requirement).

But the problem is that equals was not redefined, so when the map hashes second and iterates through the bucket looking if there is an object k such that second.equals(k) is true it won't find any as second.equals(first) will be false.

Hope it was clear

Answer from Lombo on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › equals-hashcode-methods-java
equals() and hashCode() methods in Java - GeeksforGeeks
July 23, 2025 - If two Objects are unequal, according to the equals(Object) method, It is not necessary the Integer value produced by hashCode() method on each of the two Objects will be distinct.
🌐
Codefinity
codefinity.com › blog › How-equals()-and-hashCode()-Work-in-Java-and-Why-Following-Their-Contract-Matters
How equals() and hashCode() Work in Java and Why Following Their Contract Matters
A: Yes, this is called a hash collision. Multiple objects can have the same hash code, but equals() is used as a second check to confirm actual equality. ... Learn the fundamentals of Java and its key features in this course.
Discussions

Why do I need to override the equals and hashCode methods in Java? - Stack Overflow
This method returns the hash code value as an integer and is supported for the benefit of hashing based collection classes such as Hashtable, HashMap, HashSet etc. This method must be overridden in every class that overrides the equals method. ... Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode ... More on stackoverflow.com
🌐 stackoverflow.com
Origin of misconception for equals and hashcode implementation
They probably just confused the rule that two equal objects should return the same hashcode. Therefore, it's intuitive that one might conclude equals should just use hashcode. It's one of those things that can be learned in 1-5 code reviews. It's ceremony. Java-ism trivia. Go easy on them. More on reddit.com
🌐 r/java
122
58
February 11, 2022
Java equals() and hashCode()
In Java, the equals() and hashCode() methods are crucial for working with objects in collections, such as lists, sets, and maps. These methods are part of the Object class and serve distinct purposes in ensuring proper behavior when comparing and storing objects. More on accuweb.cloud
🌐 accuweb.cloud
1
November 15, 2023
java - Do we always need to override equals/hashcode when creating a new class? - Software Engineering Stack Exchange
It can make sense to implement ... update hashcode() too if you do) and doing so doesn't prevent the use of any of the techniques I describe here. ... Use every meaningful property of the object to define equals. ... If any of these aren't followed, you are in for trouble. ... Comments are not for extended discussion; this conversation has been moved to chat. ... In Java, if you don’t ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
October 3, 2019
🌐
Baeldung
baeldung.com › home › java › core java › java equals() and hashcode() contracts
Java equals() and hashCode() Contracts | Baeldung
January 8, 2024 - In this tutorial, we’ll introduce two methods that closely belong together: .equals() and .hashCode(). We’ll focus on their relationship with each other, how to correctly override them, and why we should override both or neither. By default, the Object class defines both the .equals() and .hashCode() methods. As a result, every Java class implicitly has these two methods.:
Top answer
1 of 16
726

Joshua Bloch says on Effective Java

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, including HashMap, HashSet, and Hashtable.

Let's try to understand it with an example of what would happen if we override equals() without overriding hashCode() and attempt to use a Map.

Say we have a class like this and that two objects of MyClass are equal if their importantField is equal (with hashCode() and equals() generated by eclipse)

public class MyClass {
    private final String importantField;
    private final String anotherField;

    public MyClass(final String equalField, final String anotherField) {
        this.importantField = equalField;
        this.anotherField = anotherField;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((importantField == null) ? 0 : importantField.hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final MyClass other = (MyClass) obj;
        if (importantField == null) {
            if (other.importantField != null)
                return false;
        } else if (!importantField.equals(other.importantField))
            return false;
        return true;
    }
}

Imagine you have this

MyClass first = new MyClass("a","first");
MyClass second = new MyClass("a","second");

Override only equals

If only equals is overriden, then when you call myMap.put(first,someValue) first will hash to some bucket and when you call myMap.put(second,someOtherValue) it will hash to some other bucket (as they have a different hashCode). So, although they are equal, as they don't hash to the same bucket, the map can't realize it and both of them stay in the map.


Although it is not necessary to override equals() if we override hashCode(), let's see what would happen in this particular case where we know that two objects of MyClass are equal if their importantField is equal but we do not override equals().

Override only hashCode

If you only override hashCode then when you call myMap.put(first,someValue) it takes first, calculates its hashCode and stores it in a given bucket. Then when you call myMap.put(second,someOtherValue) it should replace first with second as per the Map Documentation because they are equal (according to the business requirement).

But the problem is that equals was not redefined, so when the map hashes second and iterates through the bucket looking if there is an object k such that second.equals(k) is true it won't find any as second.equals(first) will be false.

Hope it was clear

2 of 16
379

Collections such as HashMap and HashSet use a hashcode value of an object to determine how it should be stored inside a collection, and the hashcode is used again in order to locate the object in its collection.

Hashing retrieval is a two-step process:

  1. Find the right bucket (using hashCode())
  2. Search the bucket for the right element (using equals() )

Here is a small example on why we should overrride equals() and hashcode().

Consider an Employee class which has two fields: age and name.

public class Employee {

    String name;
    int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (!(obj instanceof Employee))
            return false;
        Employee employee = (Employee) obj;
        return employee.getAge() == this.getAge()
                && employee.getName() == this.getName();
    }

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

Now create a class, insert Employee object into a HashSet and test whether that object is present or not.

public class ClientTest {
    public static void main(String[] args) {
        Employee employee = new Employee("rajeev", 24);
        Employee employee1 = new Employee("rajeev", 25);
        Employee employee2 = new Employee("rajeev", 24);

        HashSet<Employee> employees = new HashSet<Employee>();
        employees.add(employee);
        System.out.println(employees.contains(employee2));
        System.out.println("employee.hashCode():  " + employee.hashCode()
        + "  employee2.hashCode():" + employee2.hashCode());
    }
}

It will print the following:

false
employee.hashCode():  321755204  employee2.hashCode():375890482

Now uncomment hashcode() method , execute the same and the output would be:

true
employee.hashCode():  -938387308  employee2.hashCode():-938387308

Now can you see why if two objects are considered equal, their hashcodes must also be equal? Otherwise, you'd never be able to find the object since the default hashcode method in class Object virtually always comes up with a unique number for each object, even if the equals() method is overridden in such a way that two or more objects are considered equal. It doesn't matter how equal the objects are if their hashcodes don't reflect that. So one more time: If two objects are equal, their hashcodes must be equal as well.

🌐
Reddit
reddit.com › r/java › origin of misconception for equals and hashcode implementation
r/java on Reddit: Origin of misconception for equals and hashcode implementation
February 11, 2022 -

So i do some coding interviews at my company and one question i ask nearly every Person who applies for a Java developer job is what are the equals and hashcode methods. I just want to see if the person understands what they do, how you could implement both, what contract is between them and where there are pitfalls, like on jpa entities.

It is not about how to generating those methods. I want to see if they thought about the concept.

One thing, many people say is that it is a good idea to use hashcode to implement equals. So that you should just compare the hashes.

I think it is clear that this is no good solution. But it is interesting that so many people say just that. And i think when i learned Java, i read this for myself. But i am not sure where.

Has anyone an idea where this misconception comes from? Maybe it was thought in a book or so?

🌐
Medium
medium.com › @iyusubov444 › interview-questions-java-equals-hashcode-methods-revisited-7b0cd80aa903
Interview Questions - Java equals & hashCode Methods Revisited | by Imran Yusubov | Medium
September 1, 2022 - Therefore, decided to touch this topic which is already discussed over and over. Here, I will use some concrete code examples. Let’s begin. Suppose we are given the following Java class, what is the violation here? Consider that the equals method is a valid one. At first look, it seems like everything is fine here, right? But it is not, remember the equals hashCode contract. Always override hashCode if you override equals. More on this could be found in the book “Effective Java, Methods Common to All Objects”. If we don’t override hashCode, there is no guaranty that two equals objects will have the same hashCode, unless they refer to the same object.
🌐
Medium
medium.com › @AlexanderObregon › how-javas-equals-and-hashcode-methods-work-together-da27332bb742
How Java’s equals() and hashCode() Work Together | Medium
February 22, 2025 - A HashSet uses hashCode() to determine where objects go and equals() to prevent duplicates. If two objects have the same hash code, equals() is the deciding factor in whether they should be stored separately or considered the same. let’s say ...
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-equals-hashcode
Java equals() and hashCode() | DigitalOcean
August 3, 2022 - Multiple invocations of x.equals(y) ... true only when both the references are pointing to same object. Java Object hashCode() is a native method and returns the integer hash code value of the object....
🌐
InfoWorld
infoworld.com › home › blogs › java challengers
Comparing Java objects with equals() and hashcode() | InfoWorld
May 16, 2024 - Java’s equals() and hashcode() are two methods that work together to verify if two objects have the same value. You can use them to make object comparisons easy and efficient in your Java programs.
🌐
Medium
medium.com › @AlexanderObregon › javas-tostring-equals-and-hashcode-explained-27cfc9bb1497
Java’s toString(), equals(), and hashCode() Explained | Medium
July 9, 2024 - This makes sure that the two Person objects are considered equal if and only if their name and age fields are identical. The hashCode() method returns an integer value that is used in hashing-based collections like HashMap and HashSet.
🌐
Accuweb
accuweb.cloud › home › java equals() and hashcode()
Java equals() and hashCode() - AccuWeb Cloud
November 15, 2023 - In Java, the equals() and hashCode() methods are crucial for working with objects in collections, such as lists, sets, and maps. These methods are part of the Object class and serve distinct purposes in ensuring proper behavior when comparing ...
🌐
EPAM Campus
campus.epam.com › en › blog › 339
Learning Java. equals() and hashCode() Methods | EPAM Campus article
July 21, 2020 - This approach is used when we need to store a set of unique objects, for example, in such data structure as HashMap. In this case, the equals() method is not very suitable, since it takes time to check all fields of the object. The hash code of the object is more fitting for the purpose. In Java, the object’s hashcode can be obtained using the hashCode() method.
Top answer
1 of 7
8

should we always override the equals and hashCode even if we don’t intent at that point to use the class with any Collection classes?

No and I would go even further as to say you probably don't need to override them even if you are going to put them into a collection. The default implementations are perfectly compatible with collections as is. For lists, it's irrelevant anyway.

You should override equals if and only if your objects have a logical identity that is independent of their physical identity. In other words, if you want to have multiple objects that represent the same entity. Integer, is a good example of this. The integer 2 is always 2 regardless of whether there are 100 instances or 1 of the Integer object with the value of 2.

You must override hashcode if you've overridden equals.

That's it. There are no other good reasons to modify these.

As a side note, the use of equals to implement algorithms is highly overused. I would only do this if your object has a true logical identity. In most cases, whether two objects represent the same thing is highly context dependent and it's easier and better to use a property of the object explicitly and leave equals alone. There are many pitfalls to overriding equals, especially if you are using inheritance.

An example:

Let's say you have an online store and you are writing a component that fulfills orders. You decide to override the equals() method of the Order object to be based on the order number. As far as you know, that's the it's identity representation. You query a DB every so often and create objects from the response. You take each Order object and keep it as a key in a set which is all the orders in process. But there's a problem, orders can be modified in a certain time frame. You now might get a response from the DB that contains the same Order number but with different properties.

You can't just write this object to your set because you'll lose track of what was in process. You might end up processing the same order twice. So, what are the options? You could update the equals() method to include a version number or something additional. But now you have to go through your code to figure out where you need to base the logic on the having the same order number and where you need it to be based on the new object identity. In other words, there's not just one answer to whether two objects represent the same thing. In some contexts it's the order, and in some contexts it's the order and the version.

Instead of the set, you should build a map and use the order number as the key. Now you can check for the existence of the key in the map and retrieve the other object for comparison. This is much more straightforward and easy to understand than trying to make sense of when the equals method works they way you need it to for different needs.

A good example of this kind of complexity can be found in the BigDecimal class. You might think that BigDecimal("2.0") and BigDecimal("2.00") are equal. But they are not.

Conclusion:

It can make sense to implement equals() (make sure you update hashcode() too if you do) and doing so doesn't prevent the use of any of the techniques I describe here.

But if you are doing this:

  • Make sure your object is immutable
  • Use every meaningful property of the object to define equals.
  • If you are using inheritance, either:
    1. make equals final on the base class OR
    2. you must check that the actual type matches in subclasses

If any of these aren't followed, you are in for trouble.

2 of 7
4

In Java, if you don’t override anything, equality is defined as object references being equal, and the hash code is the bit pattern of the reference. That works just fine to put objects into containers.

And there are objects where this is exactly right. For example an object representing a window in the UI. Two windows are equal if and only if they are the same window.

PS. It makes absolutely sense to have a set of windows, or a dictionary with windows as keys, or an array of windows and lookup if the array contains some window - so you need to be able to compare windows for equality.

🌐
How to do in Java
howtodoinjava.com › home › java basics › java hashcode() and equals() methods
Java hashCode() and equals() Methods - HowToDoInJava
February 23, 2023 - For this reason, all java objects inherit a default implementation of these methods. equals(Object otherObject) – verifies the equality of two objects. It’s default implementation simply checks the object references of two objects to verify ...
🌐
Project Lombok
projectlombok.org › features › EqualsAndHashCode
@EqualsAndHashCode
For the purposes of equality, 2 NaN (not a number) values for floats and doubles are considered equal, eventhough 'NaN == NaN' would return false. This is analogous to java.lang.Double's equals method, and is in fact required to ensure that comparing an object to an exact copy of itself returns ...
🌐
Softwaregarden
softwaregarden.dev › en › posts › new-java › records › should-write-equals-and-hashcode
Do we really never need to implement equals() and hashCode ...
April 14, 2021 - It says something like “record ... (…) equals and hashCode methods which ensure that two record values are equal if they are of the same type and contain equal component values» and «Developers are sometimes tempted to ...
🌐
DZone
dzone.com › data engineering › data › working with hashcode() and equals()
Working With hashcode() and equals()
January 2, 2018 - In the above implementation, we are saying that two students are equal if and only if they are stored in the same memory address OR they have the same ID. Now if we try to run HashcodeEquals, we will get the following output: alex1 hashcode = 2032578917 alex2 hashcode = 1531485190 Checking equality between alex1 and alex2 = true · As you noticed, overriding equals() with our custom business forces Java to consider the ID attribute when comparing two Student objects.
🌐
Oracle
forums.oracle.com › ords › apexds › post › what-is-hash-code-in-java-and-how-it-is-related-to-equals-m-2955
What is hash code in Java and how it is related to equals ...
August 26, 2005 - Can any body give me the detailed information about hashcode and the relationship between equals method and hash code.
🌐
CodeJava
codejava.net › java-core › collections › understanding-equals-and-hashcode-in-java
Understanding equals() and hashCode() in Java
March 9, 2024 - Basically, the Object class implements ... why are they so important in Java programming. When comparing two objects together, Java calls their equals() method which returns true if the two objects are equal, or false otherwise....