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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:02:48+00:00 2026-06-09T14:02:48+00:00

Suppose a map contains integer keys, and a list of strings as its value.

  • 0

Suppose a map contains integer keys, and a list of strings as its value. Then, I CAN’T do this :

for (Map.Entry<Integer, List<String>> entry : map.entrySet()){
    for (String string : entry.getValue()){
        if (string.startsWith("a")){
           entry.getValue().remove(string);
        }
    }
}

It throws ConcurrentModificationException. But if I do the following :

for (Map.Entry<Integer, List<String>> entry : map.entrySet()){
    entry.setValue(new ArrayList<String>());
}

This works perfectly. Aren’t we modifying the underlying map even now ?

  • 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-09T14:02:50+00:00Added an answer on June 9, 2026 at 2:02 pm

    The problem has nothing to do with the Map, only the way you’re using the value list. The following will fail with any ArrayList:

    for (String string : list){
        if (string.startsWith("a")){
           list.remove(string);
        }
    }
    

    The reason for this is discussed in the Javadoc of ArrayList:

    The iterators returned by this class’s iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

    (To put it another way: if you remove the element from the list, the iterator might not be pointing to the right index in the underlying array anymore. So instead of allowing you to use a potentially corrupted iterator, it throws a ConcurrentModificationException as a courtesy to let you know that you need to redesign your program.)

    A simple fix is to use

    Iterator<String> itr = entry.getValue().iterator();
    while (itr.hasNext()) {
      if (itr.next().startsWith("a")) {
        itr.remove();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose i have the following: boost::unordered_map< string , someValueType > map; someValueType& value =
Suppose I want to add functionality like map to a Scala List , something
I have a table with four columns, say P_Key(int), Ref_Key(int), Key(String), Value(Integer). I want
Suppose I have a Map of properties, and I'd like a String representation. toString
Can you tell me why my JS ain't working? http://prime.programming-designs.com/designs/map/ It's suppose to move
What a title, suppose I have a map like this: std::map<int, int> m; and
Suppose I have a Map: val m = Map(foo -> 10, bar -> 5)
Suppose I have a STL map where the values are pointers, and I want
I read that the Hadoop Map tasks write their output to local disk. Suppose
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')

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.