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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:09:08+00:00 2026-05-31T19:09:08+00:00

Is it possible to overwrite some HashSet element (not necessarily the one iterated) while

  • 0

Is it possible to overwrite some HashSet element (not necessarily the one iterated) while iterating over it. I want to know of a way apart from removing, editing and then re-adding it?
The reason I am asking this is because using remove while iterating always gives the java.util.ConcurrentModificationException. I figured out that there is no method to overwrite a set element.

Any help will be appreciated.

Thanks,

Somnath

  • 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-31T19:09:09+00:00Added an answer on May 31, 2026 at 7:09 pm

    You can remove an item from a Collection – including a Set – whilst iterating over it as long as you iterate over it using an Iterator and call Itertator.remove() to do the remove. However, this will only allow you to remove the current item in the iteration.

    You can’t remove another item or add one as this will cause a ConcurrentModificationException, since if you change the Set it’s not clear how you would iterate over it.

    Also, unlike a List, is doesn’t quite make sense to talk about replacing an entry in a Set. In a List items have a define order so, for example, if the second entry was “A” you could replace it with “B”. Items in a Set don’t have an order so you can’t replace one with another directly, all you can do is remove the old one and add the new one.

    Depending quite on what you want to do, your best approach might be to loop over a copy of the Set:

    Set<Object> originalSet = someMethod(); 
    
    for (Object item : new HashSet<Object>(originalSet)) {
      //do stuff which modifies originalSet
    }
    

    However, you’d have to account for the fact that the objects you iterated over would be the original values and wouldn’t reflect any changes you’d made.

    If this won’t do, then it might make sense to find another way of process the items in the Set without simply iterating over them by keeping track of which nodes you’ve processed.

    Something like this might do but could probably be improved depending on what you’re doing:

    Set<Object> originalSet = somemethod();
    
    //Keep track of items we've already processed
    Set<Object> processed = new HashSet<Object>();
    
    //toDo is used to calculate which items in originalSet aren't in processed
    Set<Object> toDo = new HashSet(originalSet);
    toDo.removeAll(processed);
    
    while (! toDo.isEmpty()) {
      //Get an object from toDo
      Object item = toDo.iterator().next();
    
      //Do some processing
      //Can update originalSet or even remove from processed if we want to re-do an item
    
      //Recalculate what's left to do
      processed.add(item);
      toDo = new HashSet(originalSet);
      toDo.removeAll(processed);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to override the Tostring() method for changing some characters. Is it possible?
I am looking for a safe and reliable way to overwrite ONE line in
I do not know if is possible in CSS, but I would like a
I'm struggling with trying to modify some Javascript code. I know what I want
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
I'm using a theme for my app to set some common layout features. One
I have some columns in PostgreSQL database that are array. I want to add
I desperately need some help on this one. I've created a <script> that closely
I'm writing an administrative panel and I want to store some of the information

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.