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

  • Home
  • SEARCH
  • 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 6390585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:33:54+00:00 2026-05-25T03:33:54+00:00

I have a HashMap mapping objects of my Context class to Integers. In the

  • 0

I have a HashMap mapping objects of my Context class to Integers. In the Context class, I did override the public int hashCode() and public boolean equals(Object c) of java.lang.Object. However, I have problems iterating through it:

I want (e.g.) to get the Integer value assigned to each Context object, so I iterate through the key set of the map. But it doesn’t work, because the map says it doesn’t have the specified key:

for (Context to : map.keySet()) {
    System.out.println("to-hash: " + to.hashCode());
    System.out.println("first-hash: " + map.keySet().iterator().next().hashCode());
    System.out.println("hashs equal: " + (to.hashCode()==map.keySet().iterator().next().hashCode()));
    System.out.println("to equals first: " + to.equals(map.keySet().iterator().next()));
    System.out.println("map has to? " + map.containsKey(to));
}

The output is

to-hash: 156349
first-hash: 156349
hashs equal: true
to equals first: true
map has to? false

From what I understand is that when given a key, the map first checks if the hash codes match, and then checks for equality. Both is the case here, the hash code of the ‘to’ object and the first object in the key set match and they are also equal. Interestingly, when I change the return value of the hashCode() function to a constant (which is valid, though not recommended for performance reasons), it works. But I don’t see why this makes a difference, since 156349==156349 just like 7==7.

I’m quite confused, and I’m afraid that I’m missing something very obvious, and just can’t see it. If that’s the case, shame on me, but still, I would appreciate a hint 🙂

Thanks a lot!

  • 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-25T03:33:54+00:00Added an answer on May 25, 2026 at 3:33 am

    This could happen if your Context object is mutable in a way that affects the hash code, and if you’ve performed an operation which changes the hash code after putting it in the map. The map will only record the value of hashCode() at the point of insertion and then will use that to find matches when you try to look up a particular key.

    This would be consistent with it working if you make the hash function a constant. Basically, you shouldn’t mutate hash keys after putting them in a map.

    Of course, this is just conjecture, but it does fit the symptoms.

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

Sidebar

Related Questions

I have a HashMap object that I am getting on a JSP page. HashMap<Integer,Gift_product>
At the moment I have val orders = new HashMap[Int, Int] orders.put(36, 110) orders.put(35,
I have a HashMap of a following structure: HashMap<Integer, int[]> map = new HashMap<Integer,
I have HashMap object contains a key x-y-z with corresponding value test-test1-test2 . Map<String,String>
I have a HashMap having key as my own object and key as ArrayList
I decided to do server-side sorting. I have a HashMap with Objects (key is
I need to represent a many-many object mapping between two different groups of objects
I have a 2 enum values representing a mapping to object which I'm (currently)
I have HashMap 1, which contains 5 keys, all of which have Hashmaps as
I have a HashMap that I am serializing and deserializing to an Oracle db,

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.