Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 610475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:40:46+00:00 2026-05-13T17:40:46+00:00

Recently I read through this Developer Works Document . The document is all about

  • 0

Recently I read through this
Developer Works Document.

The document is all about defining hashCode() and equals() effectively and correctly, however I am not able to figure out why we need to override these two methods.

How can I take the decision to implement these methods efficiently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:40:46+00:00Added an answer on May 13, 2026 at 5:40 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 314k
  • Answers 314k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's a long and complicated piece of code, which I'm… May 13, 2026 at 11:04 pm
  • Editorial Team
    Editorial Team added an answer I won't claim to fully understand your scenario, but it… May 13, 2026 at 11:04 pm
  • Editorial Team
    Editorial Team added an answer What you're doing in your code will not work. However,… May 13, 2026 at 11:04 pm

Related Questions

Okay, thus may seen kind of odd, but I wanted to get some suggestions
I am just wondering how some things work in gamedev: I know, that the
RPO 1.0 (Runtime Page Optimizer) is a recently (today?) released component for ASP and
I've recently started getting into Git on a personal project, and I can see
I am using Eclipse 3.4.2 to develop my code. As part of my project

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.