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

The Archive Base Latest Questions

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

I am creating 3 objects of same class: A,B,rB. Here rB is referencing to

  • 0

I am creating 3 objects of same class: A,B,rB. Here rB is referencing to B. And values of A and B are equal. I had overrided equals method where I am comparing the values.

So

A.equals(B), A.equals(rB) and B.equals(rB) is true. But A != B, A !=
rB and B == rB.

Now I am putting A,B,rB in HashMap, say hm, and IdentityHashMap, say ihm.

    hm.put(A, "1");
    hm.put(B, "2");
    hm.put(rB, "3");

    ihm.put(A, "1");
    ihm.put(B, "2");
    ihm.put(rB, "3");

Since I am not storing null, so hm.get(A) should return 3, hm.get(B) should return 3 and hm.get(rB) should return 3.
Similarly, ihm.get(A) should return 1, ihm.get(B) should return 3 and ihm.get(rB) should return 3.

As per the java docs, IdentityHashMap does k1 == k2 while HashMap does k1.equals(k2) , if k1 and k2 are not null.

So why hm.get(A) is returning 1.

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

    You say that you overrode equals, but did you override hashCode as well? If not, then this is likely to be the cause of the behaviour you are seeing.

    HashMap will only use equals when two keys have the same hash code, so it is essential that whenever you override equals you also override hashCode and vice versa.

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

Sidebar

Related Questions

I'm at a bit of a loss here. I'm creating a collection of objects
When creating jQuery objects I've used the following syntax lately, as described here :
When creating 2 objects of the same type, will the handle from the stack
I am creating QueryDSL objects for MongoDB using Maven, here's the build xml, <build>
I feel like I skipped a C# class or two, but here's my dilemma:
What's the best framework for creating mock objects in Java? Why? What are the
How can this be done more elegant and without creating new objects? List<Move> playerMoves
nobody loved my first question about this: Creating Entity Framework objects with Unity for
I'm writing a program and I seem to be creating alot of objects where
Prior to C# generics, everyone would code collections for their business objects by creating

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.