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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:22:52+00:00 2026-06-17T15:22:52+00:00

I have a map that maps bytes to sets of bytes. I want to

  • 0

I have a map that maps bytes to sets of bytes. I want to walk through the map and make changes to the set.

private HashMap<Byte, HashSet<Byte>> table;
...
Iterator<Entry<Byte, HashSet<Byte>>> it = table.entrySet().iterator();
while( it.hasNext() ) {
   Map.Entry<Byte, HashSet<Byte>> pairs = it.next();
   byte node = pairs.getKey();
   HashSet<Byte> hSet = pairs.getValue();
   Iterator<Byte> setIter = hSet.iterator();
   while( setIter.hasNext() ) {
      byte sNode = setIter.next();  // Throws a ConcurrentModificationException
      ...
   }
 }

This code throws a ConcurrentModificationException when I try to iterate through the sub iterator. What should I do to iterate through, and make changes to, this collection within my map?

  • 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-17T15:22:54+00:00Added an answer on June 17, 2026 at 3:22 pm

    I think you get the exception because you modify the set during the iteration (in the lines that were not included into the question). The “fail-fast” iterators are throwing it, if they detect that the structure of the collection has been changed.

    Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.

    http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html

    If you are only deleting from the set while you iterate it, you can use the remove method of the iterator instead of the Set methods. If you also want to add to the set while iterating, you need to create a copy of the Set, and iterate that (but this case is unlikely, you can add to a set without iterating it…)

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

Sidebar

Related Questions

I have successfully set up a Leaflet JS map that uses the Google maps
I have a map that i want to update from a separate thread. Im
I have a map that represents a DB object. I want to get 'well
I have a Map that uses a Set for the key type, like this:
I have a static map that is a private data member. How do I
I have a class with a static std::map member variable that maps char s
What I would like to do is have a map that maps integer keys
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
Right now I have google map code that will set a single marker on
I have a Map of Maps that looks like this: Map<String, Map<String, Object>> data;

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.