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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:43:23+00:00 2026-05-25T13:43:23+00:00

I found this statement if a thread modifies a collection directly while it is

  • 0

I found this statement 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. at http://download.oracle.com/javase/6/docs/api/java/util/ConcurrentModificationException.html.

I found the concurrent modification is even thrown in below code

List<Employee> lista= new ArrayList();
Employee emp1=new Employee();
Employee emp2=new Employee();
Employee emp3=new Employee();
lista.add(emp1);
lista.add(emp2);
lista.add(emp3);
for(Employee emp:lista)
{
emp2.setEmpId(2);
lista.remove(emp2);
}

Question1:- So can i say enhance for loop also uses the fail-fast iterator internally?Though when i execute below code it works fine

for(int i=0;i<lista.size();i++)
{
Employee empTemp=lista.get(i);
lista.remove(emp2);         
}

Question2:- Another question is when statement say

if a thread modifies a a collection

My guess is that modification means here removal or addition not for update of element inside collection for list interface while it also includes modification of element for set interface . Right? At least the programmes i tried it is the case with them.

Edit

Regarding set i doubt my above statement i.e it throws concurrent modification exception when we try to modify set while iterating.I tried below code but it did not throw any exception

HashSet<Employee> set1= new HashSet();
Employee emp4=new Employee();
Employee emp5=new Employee();
Employee emp6=new Employee();
set1.add(emp4);
set1.add(emp5);
set1.add(emp6);


Iterator iter1=set1.iterator();
while(iter1.hasNext())
{
Employee emp12=(Employee)iter1.next();
System.out.println("");
emp5.setEmpId(2);

}

Ideally as per statement if the set is modified at any time after the iterator is created at
http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html it should throw the concurrent modification exception but it did not . Not sure why?

  • 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-25T13:43:23+00:00Added an answer on May 25, 2026 at 1:43 pm

    Question1:- So can i say enhance for loop also uses the fail-fast iterator internally?Though when i execute below code it works fine

    Yes, thats right. Have a look at the compiled code, with the javap command to verify this if you like.

    My guess is that modification means here removal or addition not for updation of element inside collection for list interface while it also includes modification of element for set interface . Right? Atleast the programmes i tried it is the case with them.

    Thats right, if you do emp1.setEmpId(2) or something similar, the iteration will not fail.

    …it should throw the concurrent modification exception but it did not . Not sure why?

    It only throws the exception if you modify the list. Keep in mind that the list contains references to objects. If you modify the objects, the references does not change, thus the list does not change.

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

Sidebar

Related Questions

I found this statement is some old code and it took me a second
I was reading about parsers and parser generators and found this statement in wikipedia's
In the java docs of the map interface's entrySet() method I found this statement
Reading this question I found this as (note the quotation marks) code to solve
I found this open-source library that I want to use in my Java application.
I found this link http://artis.imag.fr/~Xavier.Decoret/resources/glsl-mode/ , but there isn't a lot of description around
I found this in an article on Multithreaded Apartments, but can’t find a definition
Just found this out, so i am answering my own question :) Use a
I found this guide for using the flash parameters, thought it might be useful
I found this PECL package called threads , but there is not a release

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.