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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:09:13+00:00 2026-06-14T12:09:13+00:00

In my application I have used a Map to store POJO objects. As per

  • 0

In my application I have used a Map to store POJO objects. As per the requirement I need to iterate over the keySet of the Map and remove objects which dont need any modification.

Conside the code below:

 public void remove(Map<String,User> removeUser){
  Set<String> keySet = removeUser.keySey();
  User user = null;

  for(String key : keySet){
      user = (user) removeUser.get(key);

       if(!user.isActive()){
                removeUser.remove(key);
       }
  }

 }

Here in above code, I am getting ConcurrentModificationException when I try to fetch User object after Object removal.

Can anyone tell me why it’s happening?

I have not used multi threading.So not able to understand, from where it generated ConCurrentModification Exception.

Even I tried with HashMap and Hashtable, but the problem still exist.

  • 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-14T12:09:14+00:00Added an answer on June 14, 2026 at 12:09 pm

    from where it generated ConCurrentModification Exception.

    It came from the line where you are trying to remove the element from your Map while iterating over its KeySet.

    if(!user.isActive()){
         removeUser.remove(key);  // This is the problem
    }
    

    You should not do that. If you want to modify your Collection or Map, use an iterator.


    See this very nice post – efficient-equivalent-for-removing-elements-while-iterating-the-collection explaining the problems that can come while modifying the collection you iterate upon.


    Here’s a simple code explaining how you can use it here: –

        Map<String, Integer> map = new HashMap<String, Integer>() {
            {
                put("a", 1);
                put("b", 2);
                put("c", 3);
            }
        };
    
        Iterator<String> iterate = map.keySet().iterator();
    
        while  (iterate.hasNext()) {
            int i = map.get(iterate.next());
    
            if(i > 1) {
                iterate.remove();
            }
        }
    
        System.out.println(map);
    

    OUTPUT: –

    {a=1}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my android application i have used the google map. I have using android
I have developed an application on android.I have used Map overlay class in application.When
I have an application which used top bar navigation and a MapView. On the
i have made an application in which i need to load the google maps.
I see an application have used Log.info = some info where are these logs
I'm working on a Phonegap application.I have used header for every page. But when
I have integrate draw line in my application i have not used OpenGL or
i have developed application for read information from card reader. Here i have used
I have used AsyncTasks with my application, in order to lazy download and update
i have used impersonation in this application. whenever this error occurs i required to

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.