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 8963017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:16:01+00:00 2026-06-15T16:16:01+00:00

Possible Duplicate: In Java, why must equals() and hashCode() be consistent? I read that

  • 0

Possible Duplicate:
In Java, why must equals() and hashCode() be consistent?

I read that one should always hascode() when overriding equals().

Can anyone give a practical example of why it might be wrong otherwise?
i.e. Problems that might arise when overriding equals() but not hashCode().

Is it necessary to write a robust hasCode() function whenever we override equals()? Or a trivial implementation is enough?

For example,

A poor implementation such as the below is good enough to satisfy the contract between equals() & hashCode()?

public int hashCode() {
   return 91;
}
  • 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-06-15T16:16:02+00:00Added an answer on June 15, 2026 at 4:16 pm

    equals() and hashCode() are used conjunctively in certain collections, such as HashSet and HashMap, so you have to make sure that if you use these collections, you override hashCode according to the contract.

    If you don’t override hashCode at all, then you’ll have problems with HashSet and HashMap. In particular, two objects that are “equal” may be put in different hash buckets even though they should be equal.

    If you do override hashCode, but do so poorly, then you’ll have performance issues. All your entries for HashSet and HashMap will be put into the same bucket, and you’ll lose the O(1) performance and have O(n) instead. This is because the data structure essentially becomes a linearly-checked linked list.

    As for breaking programs outside of these conditions, it’s not likely, but you never know when an API (especially in 3rd-party libraries) is going to depend on this contract. The contract is upheld for objects that don’t implement either of them, so it’s conceivable that a library may depend on this somewhere without using hash buckets.

    In any case, implementing a good hashCode is easy, especially if you’re using an IDE. Eclipse and Netbeans both have the ability to generate equals and hashCode for you in a way that all contracts are followed, including the inverse rules of equals (the assertion that a.equals(b) == b.equals(a)). All you need to do is select the fields you want to be included and go.

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

Sidebar

Related Questions

Possible Duplicates: In Java, why must equals() and hashCode() be consistent? Why hashCode() can
Possible Duplicate: Java: Always override equals? should I override equals function for any class
Possible Duplicate: Java 7 Date/Time API I've read rumors that Joda Time is slated
Possible Duplicate: Java String.equals versus == I know it' a dumb question but why
Possible Duplicate: Java float division precision I'm trying to fix a bug in one
Possible Duplicate: Java : Is there a good natural language processing library Can anybody
Possible Duplicate: Java: how to check that a string is parsable to a double?
Possible Duplicate: Java modifiers syntax and format You can put modifiers of variables or
Possible Duplicate: java PreparedStatement Can I make the prepared statement SELECT * FROM STUDENTS
Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” When should you use: class MyThread

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.