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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:07:00+00:00 2026-05-13T20:07:00+00:00

Is this a valid way to find and remove item from a LinkedList in

  • 0

Is this a valid way to find and remove item from a LinkedList in Java using a for each loop, is it possible that inconsistency may arise:

for(ObjectType ob : obList) {
  if(ob.getId() == id) {
    obList.remove(ob);
    break;
   }
}
  • 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-13T20:07:00+00:00Added an answer on May 13, 2026 at 8:07 pm

    Others have mentioned the valid point that normally this is not how you remove an object from a collection. HOWEVER, in this case it’s fine since you break out of the loop once you remove.

    If you want to keep iterating after a remove, though, you need to use an iterator. Otherwise you’ll get a ConcurrentModificationException, or in the more general case, undefined behavior.

    So yes, if you break out of the foreach after you remove, you’ll be fine.


    To those who’s saying that this will fail because you can’t modify a collection in a foreach — this is true only if you want to keep iterating. That’s not the case here, so this shortcut is fine.

    A ConcurrentModificationException is checked and thrown by the iterator. Here, after the remove (which qualifies as concurrent modification), you break out of the loop. The iterator doesn’t even get a chance to detect it.

    It may be best if you add a comment on the break, why it’s absolutely necessary, etc, because if this code is later modified to continue iterating after a remove, it will fail.

    I would treat this idiom similar to goto (or rather, labeled break/continue): it may seem wrong at first, but when used wisely, it makes for a cleaner code.

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

Sidebar

Related Questions

Possible Duplicates: Test for value in Javascript Array Best way to find an item
Is this a valid way to do performance analysis? I want to get nanosecond
Is this the correct (or even a valid way) to use emums in Objective-C?
Is there any way to format this so it's a valid expression, without adding
When an object returns false for .valid? , is there a way to find
In a jsp file, is there a way to find a component on that
I need to find a way to format the string such that I can
I've been writing a C++ program that will eventually remove certain undesired formatting from
I can't find a valid way with Django OMR in order to get :
Need: Find a way to add a valid tag/attribute/property to a normal html control.

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.