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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:22:34+00:00 2026-05-13T09:22:34+00:00

I know that it’s typically a big no-no to remove from a list using

  • 0

I know that it’s typically a big no-no to remove from a list using java’s “foreach” and that one should use iterator.remove(). But is it safe to remove() if I’m looping over a HashMap’s keySet()? Like this:

for(String key : map.keySet()) {
  Node n = map.get(key).optimize();
  if(n == null) {
   map.remove(key);
  } else {
   map.put(key, n);
  }
}
  • 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-13T09:22:34+00:00Added an answer on May 13, 2026 at 9:22 am

    EDIT:

    I hadn’t noticed that you weren’t really adding to the map – you were just changing the value within the entry. In this case, pstanton’s (pre-edit1) solution is nearly right, but you should call setValue on the entry returned by the iterator, rather than calling map.put. (It’s possible that map.put will work, but I don’t believe it’s guaranteed – whereas the docs state that entry.setValue will work.)

    for (Iterator<Map.Entry<String, Node>> it = map.entrySet().iterator(); 
         it.hasNext();)
    {
        Map.Entry<String, Node> entry = it.next();
        Node n = entry.getValue().optimize();
        if(n == null) 
        {
            it.remove();
        }
        else
        {
            entry.setValue(n);
        }
    }
    

    (It’s a shame that entry doesn’t have a remove method, otherwise you could still use the enhanced for loop syntax, making it somewhat less clunky.)

    Old answer

    (I’ve left this here for the more general case where you just want to make arbitrary modifications.)

    No – you should neither add to the map nor remove from it directly. The set returned by HashSet.keySet() is a view onto the keys, not a snapshot.

    You can remove via the iterator, although that requires that you use the iterator explicitly instead of via an enhanced for loop.

    One simple option is to create a new set from the original:

    for (String key : new HashSet<String>(map.keySet())) {
        ...
    }
    

    At this point you’re fine, because you’re not making any changes to the set.

    EDIT: Yes, you can definitely remove elements via the key set iterator. From the docs for HashMap.keySet():

    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.

    This is even specified within the Map interface itself.


    1 I decided to edit my answer rather than just commenting on psanton’s, as I figured the extra information I’d got for similar-but-distinct situations was sufficiently useful to merit this answer staying.

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

Sidebar

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.