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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:07:19+00:00 2026-06-10T09:07:19+00:00

In HashSet.java in JDK 1.6, there are some comments about the fail-fast property of

  • 0

In HashSet.java in JDK 1.6, there are some comments about the fail-fast property of HashSet’s iterator.

The iterators returned by this class’s iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the Iterator throws 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.

I can understand the above paragraph because It’s pretty simple and clear, but I cannot understand the following paragraph. I may understand it if I have some simple examples showing that a fail-fast iterator can even fail.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

  • 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-10T09:07:21+00:00Added an answer on June 10, 2026 at 9:07 am

    EDIT: Sorry I used a list, but it’s the same idea. This is about iterator, not the Collection behind it.

    EDIT2: Also this is far more likely to happen in a multi-threaded environment, where you have two threads, one reading, one writing. These are harder to see when you are coding. To fix those you need to implement Read/Write locks on the list to avoid this.

    Here is a code example for the comment:

    Iterator itr = myList.iterator();
    
    while(itr.hasNext())
    {
        Object o = itr.next();
    
        if(o meets some condition)
        { 
            //YOURE MODIFYING THE LIST
            myList.remove(o);
        }
    }
    

    What the specs is saying is that you cannot rely on code like this:

    while(itr.hasNext())
    {
         Object o = itr.next();
    
         try
         {
             if(o meets some condition)
                myList.remove(o);
         }
         catch(ConcurrentModificationException e)
         {
             //Whoops I abused my iterator. Do something else.
         }
    }
    

    Instead you should probably add things to a new list and then switch the myList reference to the one just created. Does that explain the situation?

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

Sidebar

Related Questions

My xml looks like this: <bean name=subscriberStore class=java.util.HashSet scope=singleton/> And I have the following
Let's say I have this code in Java: HashSet<String> wordSet = new HashSet<String>(); String
Java Tutorials (Set Implementations) : One thing worth keeping in mind about HashSet is
private ArrayList<HashSet<Integer>> sets = new ArrayList<HashSet<Integer>>(); Iterator i = this.sets.iterator(); while (i.hasNext()){ if(i.next().containsAll(union)){ return
Newbie question about java HashSet Set<User> s = new HashSet<User>(); User u = new
I'm using Spring to load a Set containg integers: <util:set id=ModifiableTags set-class=java.util.HashSet> <value>44</value> <value>38</value>
Spring: In my context.xml, I have: <util:set id=someIDs set-class=java.util.HashSet value-type=java.lang.String> <value>W000000001</value> <value>W000000003</value> <value>W000000009</value> </util:set>
Apparently, there are two ways to obtain a thread-safe HashSet instance using Java’s Collections
Today I ran into some weird behaviour with a HashSet's iterator. In the code
So, if I try to remove elements from a Java HashSet while iterating, I

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.