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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:20:49+00:00 2026-06-10T02:20:49+00:00

I`m having some trouble figuring out how to implement a working hashcode for the

  • 0

I`m having some trouble figuring out how to implement a working hashcode for the below example:

I have 2 classes :

Class A
{
} 

Class B 
{
  protected List<A> constitutingObjects;
}

Already overridded equal so object A equals object B if A is part of B.constitutingObects.
The issue I have is that i`m not sure how to implement the Hashcode for this.

Any ideas ?

Thanks.

  • 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-10T02:20:50+00:00Added an answer on June 10, 2026 at 2:20 am

    TL;DR: What you’re trying to do doesn’t make sense. Take a step back and try to tackle your bigger task in a different way.

    Already overridded equal so Class A equals Class B if A is part of B.constitutingObects

    That sounds like a very bad idea. It’s hard to tell what these objects are meant to represent, but something containing a collection isn’t logically equal to an element of that collection – a shopping list isn’t equal to “milk”.

    Don’t forget that you must follow the requirements specified by the java.lang.Object documentation. Lots of other code will depend on those guarantees.

    Implementing hashCode will basically be impossible unless you just return a constant. The hash code of two equal objects has to be equal, which means the hash code of any instance of ClassB would have to be equal to the hash code of every element of constitutingObjects, which means that all of those elements would have to have the same hash code. Unless instances of ClassA somehow “knew” what their container was, I don’t see how this could be possible.

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

Sidebar

Related Questions

I'm having some trouble figuring this out. I have an unordered list menu that
I'm having some trouble figuring out to call methods that I have in other
I having some trouble figuring this problem out where I have one button which
Having some trouble figuring out why my ImageView buttons stop working after I change
I am working on a REST based API, and having some trouble figuring out
I'm having some trouble figuring out how to require a class has extended an
I'm having some trouble figuring out the right way to do this: I have
I have been having some trouble figuring out how exactly I get a process's
I'm having some conceptual trouble on figuring out how to best implement this... I
I am having some trouble figuring out why I am only receiving one reply

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.