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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:07:20+00:00 2026-05-27T21:07:20+00:00

What are the worst case consequences (real world scenarios here), if I have an

  • 0

What are the “worst case” consequences (real world scenarios here), if I have an object (i.e. a bean) which has a solid, meaningful equals() method, but lacks a complementary hashCode() method? It seems like most APIs use the equals() and compareTo() methods for maintaining collections. I’m wondering when is the hashCode() most important?

  • 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-27T21:07:20+00:00Added an answer on May 27, 2026 at 9:07 pm

    The worst-case consequences are that the hash table won’t work. For example, lets assume that you’ve got a simple class like this:

    public class StringWrapper {
        private String value;
        ...
        public boolean equals(Object other) {
            // two objects are equal if their respective values are equal ... using `equals`.
        }
        // No hashcode override.
    }
    

    Here we have a class we have a class does not obey one of the required invariants of the equals/hashcode contract. Specifically, two instances cal be equal, but have different hashcodes.

    When you add two different instances of this class to a HashSet, you may end up with both instances in the set … on different hash chains. You get similar anomalies when removing objects from the set, testing to see if the set contains an object, and so on.

    (Note that you may get lucky, and have the two instances end up on the same hash chain despite having different hashcodes. But your luck could change when some other unrelated object is added to the hash table, causing it to automatically resize. The resize is liable to cause entries with different hashcodes to be redistributed to different hash chains.)


    So I’m wondering… when is the hashcode most important ?

    It is always important.

    Or to put it another way, it is only unimportant when you know with 100% certainty that instances of the class are never ever going to be used in hash tables. (And I don’t know how you can know that, unless you delete all copies of the class. And that renders the whole question moot!)


    UPDATE

    @supercat mentioned the case where you can’t write a decent hashcode.

    If there is some good reason you can’t write a half-decent hashcode() method, I’d advocate overriding it to throw UnsupportedOperationException or some such. That way you get a fast-fail if someone tries to put an instance into a hash table … rather than a mysterious performance black-hole. (I would NOT recommend returning a constant hashcode.)

    There is a related scenario where you might do this: when the object is inherently mutable, and you want to avoid the badness that happens if you mutate a hash key so that its hashcode changes.

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

Sidebar

Related Questions

I'm looking for an intuitive, real-world example of a problem that takes (worst case)
I have a query which in the worst case will create upwards of 150K
I would also like to know which algorithm has the worst case complexity of
Obviously best-case is O(n), but apparently the worst-case is O(n 2 ), which I
1-)For sorted array I have used Binary Search. We know that the worst case
I'm currently exploring worst case scenarios of atomic commit protocols like 2PC and 3PC
How are algorithms analyzed? What makes quicksort have an O(n^2) worst-case performance while merge
Heap Sort has a worst case complexity of O(nlogn) while Quicksort has O(n^2) .
Simplex algorithm is said to have exponential worst case time complexity. Yet it is
Is it possible for an algorithm/program to have the same worst-case and best-case time?

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.