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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:39:12+00:00 2026-05-28T00:39:12+00:00

I get Concurrent Modification Exception when i try to remove an item from HashMap.

  • 0

I get Concurrent Modification Exception when i try to remove an item from HashMap. I know that removing items during iteration through HashMap will trigger this exception , but i need to replace older item with new one. How can i do this ? Maybe to create a copy of countNumberOfEachCharacter HashMap , and whe iterate through original HashMap to remove item from copy HashMap ?

countNumberOfEachCharacter = new HashMap<Character,Character>();
if (countNumberOfEachCharacter.containsKey(word.charAt(i))) {
    System.out.println("This character already exists");                     
    for (Iterator it = countNumberOfEachCharacter.entrySet().iterator(); it.hasNext();) {

      Map.Entry entry = (Map.Entry) it.next();

      Object key = entry.getKey();
      Object value = entry.getValue();

      if (key.equals(word.charAt(i))) { 

        int toIncrease = Integer.parseInt(value.toString());
        toIncrease++;

        System.out.println("key  "+key);                                                     
        System.out.println("increased  "+toIncrease);                                                       
        countNumberOfEachCharacter.remove(word.charAt(i));

        char c = Character.forDigit(toIncrease, 10);                                                     
        countNumberOfEachCharacter.put(word.charAt(i),c);                                                                                                                                                                               
    }                                                                                                                                                                                                           
  }                                                                           
}
else {    

   System.out.println("First time found this character");

   char c = Character.forDigit(1, 10);                                
   countNumberOfEachCharacter.put(word.charAt(i),c);                            
   System.out.println("Stored "+word.charAt(i)+" with count "+c);                                                               
}
  • 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-28T00:39:13+00:00Added an answer on May 28, 2026 at 12:39 am

    …but i need to replace older item with new one

    I take it from “replace” (and from the code you’ve quoted) that the key remains the same, it’s just the value that differs. If so, I don’t believe calling setValue on Map.Entry objects causes a ConcurrentModificationException, so you could do that.

    Update: Just tested it, and indeed, it works:

    import java.util.*;
    
    public class ReplaceMapEntryValue {
    
        public static final void main(String[] args) {
            Map m;
            Iterator<Map.Entry> it;
            Map.Entry entry;
    
            // Create
            m = new HashMap();
            m.put("a", "alpha");
            m.put("b", "beta");
    
            // Update
            it = m.entrySet().iterator();
            while (it.hasNext()) {
                entry = it.next();
                if (entry.getKey() == "b") {
                    entry.setValue("bravo");
                }
            }
    
            // Show
            it = m.entrySet().iterator();
            while (it.hasNext()) {
                entry = it.next();
                System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
            }
    
            // Done
            System.exit(0);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get a URL from a user. I need to know: a) is the
Hi I could use some help fixing the Concurrent Modification Exception I'm getting, I
How to avoid concurrent modification exception in the following code? BeanList is a list
We get a large amount of data from our clients in pdf files in
System.Collections.Concurrent has some new collections that work very well in multithreaded environments. However, they
I working on app what should get list of users from db and update
I'm getting the following errors: Exception in thread main java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810) at java.util.HashMap$KeyIterator.next(HashMap.java:845)
I am creating a map application that receives a list of coords from a
I want to remove strings of length 5 from a set, but it keeps
Using Spring Security 3.1.0, I cannot seem to get the concurrent session control feature

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.