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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:24:35+00:00 2026-06-10T13:24:35+00:00

When you Iterate a collection using Java 1.5 modern for loop and remove some

  • 0

When you Iterate a collection using Java 1.5 modern for loop and remove some element
concurrentmodifuicationexception is thrown.

But when I run follwoing code it does not throw any exception:

    public static void main(String a []){
          Set<String> strs = new HashSet<String>();
          strs.add("one");
          strs.add("two");
          strs.add("three);

          for(String str : strs){
                   if(str.equalsIgnoreCase("two"){
                          strs.remove(str);
                   }
          }  
    }   

Above code does not throw ConcurrentModificationException. But when I use any such for loop in my web application service method it always throws one. Why? I am sure no two threads are accessing the collection when it runs in service method So what causes the difference in two scenarios where it is thrown in one and not in the other?

  • 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-10T13:24:37+00:00Added an answer on June 10, 2026 at 1:24 pm

    I get a ConcurrentModificationException when running your code (after fixing the few typos).

    The only scenarios where you would not get a ConcurrentModificationException are:

    • if the item that you remove is not in the set, see example below:
    • if you remove the last iterated item (which is not necessarily the last added item in the case of a HashSet)
    public static void main(String[] args) {
        Set<String> strs = new HashSet<String>();
        strs.add("one");
        strs.add("two");
        strs.add("three");
    
        for (String str : strs) {
            //note the typo: twos is NOT in the set
            if (str.equalsIgnoreCase("twos")) {
                strs.remove(str);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java I can iterate collection and remove some objects from it using Iterator.remove()
I am using an Iterator to iterate through a collection and I want to
In Java, is it legal to call remove on a collection when iterating through
Using Struts 1.3. Submitting forms as a collection using the <logic:iterate> tag I'd like
I'm using JSF/Facelets, and I'm trying to iterate over some Document objects (custom object)
I am using this.collection.each() to iterate through the collection fetched from the backend. Problem:
I'm trying to iterate over a collection of entity objects using ui:repeat tags. While
Is there a way to iterate (through foreach preferably) over a collection using reflection?
Using Access 2010, WinXP. I'm trying to iterate through a collection of folders, collecting
I have a method with a nested foreach collection (iterate a set of objects

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.