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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:52:38+00:00 2026-06-06T23:52:38+00:00

I am trying to iterate on HashMap and rewrite some of the elements to

  • 0

I am trying to iterate on HashMap and rewrite some of the elements to the other map, but I have following problem:

@Test
public void test() {
    Map<SubClass, String> map = new HashMap<SubClass,String>();
    Map<SubClass, String> anotherMap = new HashMap<SubClass,String>();
    map.put(new SubClass(), "10");

    for(SubClass i : map.keySet()) {
        System.out.println(i); // initial (because toString is implemented)
        System.out.println(map.get(i)); // 10
        // here it's ok...

        i.name="another";

        System.out.println(i); // another
        System.out.println(map.get(i)); // null!
        // but here it occurs that  map.get(i) returns null!

        anotherMap.put(i, map.get(i));
    }
    for(SubClass i : anotherMap.keySet()) {
        System.out.println(i); // another
        System.out.println(map.get(i)); // null!
    }
}
// SubClass has String name; and hashCode and equals implemented

According to javadoc:

java.util.Map.keySet()

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the
map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in
progress (except through the iterator’s own remove operation), the results of the iteration are
undefined. The set supports element removal, which removes the corresponding mapping from the map,
via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the
add or addAll operations.

It says “changes to the map are reflected in the set, and vice-versa”. So why does it behave this way and most important: how can I overcome it to make both maps contain only modified key and non-null value?

UPDATE:
My friend did this test on java 1.5.0.19 (I have 1.7.0_03, and the same occurs on 1.5.0_21) and got correct output:

initial
10
another
10

UPDATE2:
Oh, he didn’t implement hashCode/equals, so first update is irrelevant

  • 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-06T23:52:39+00:00Added an answer on June 6, 2026 at 11:52 pm

    You’re modifying the key, not the map. There is no way for Map<K,V> to detect that you have changed the object within it. To make the map “see” the change you will need to call remove(originalKey), change the key, then call put(modifiedKey,object).

    Modifying the map would be a call to clear, put, putAll, or remove.

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

Sidebar

Related Questions

I'm trying to iterate over a map in c++ using openMP, but I got
I am having a Map of type HashMap. I am trying to iterate over
I am trying to iterate through array of objects in PHP, but I can't
I'm trying to iterate over the items on my start menu, but I keep
I am trying to iterate through a collection of elements, trying to locate a
So, I'm trying to use a HashMap to map my own Object to a
I am trying to iterate JSONArray in velocity template but its not working I
I am trying to iterate and print using the iterator pattern but i get
I need help. When trying iterate over a set I get the following error:
Im trying to iterate this process..using the for-loop method in jquery (each();) but I

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.