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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:37:47+00:00 2026-06-06T00:37:47+00:00

I played around with java.util.HashMap to learn what the fail-fast behaviour is. HashMap map

  • 0

I played around with java.util.HashMap to learn what the fail-fast behaviour is.

HashMap map = new HashMap();
map.put("jon", 10);
map.put("sean", 11);
map.put("jim", 12);
map.put("stark", 13);
map.put("vic", 14);
Set keys = map.keySet();
for(Object k:keys) {
    System.out.println(map.get(k));
}

for(Object k:keys) {
   String key =(String)k;
   if(key.equals("stark")) {
      map.remove(key);
    }
}

System.out.println("after modifn");
for(Object k:keys) {
    System.out.println(map.get(k));
}

I got the result

12
11
10
14
13
after modifn
12
11
10
14

I also tried using an iterator

Iterator<String> itr = keys.iterator();
while(itr.hasNext()) {
    String key = itr.next();
    if(key.equals("stark")) {
        map.remove(key);
    }
}

I didn’t get any ConcurrentModificationException in either case ..Is this because (from javadoc)

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

I checked another thread which says ,it WILL throw ConcurrentModificationException..what do you think?

  • 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-06T00:37:50+00:00Added an answer on June 6, 2026 at 12:37 am

    Given the output that you have shown:

    12
    11
    10
    14
    13   // notice this?
    after modifn
    12
    11
    10
    14
    

    Since 13 is the last key-value pair, when you Iterate through your HashMap and then finally remove the key-value corresponding to stark 13, that stops the Iteration just after the HashMap has been modified, hence, it doesn’t iterate anymore. So no ConcurrentModificationException.

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

Sidebar

Related Questions

I'm kinda new to Multi-Threading and have only played around with it in the
I am fairly new to writing code, but I played around with some chrome
I've played around with the netbeans visual editor for java and it seems very
I'm building a video player in pure Java around JMF, with completely custom UI
I've successfully played around with some of the color settings in the Git Bash
I've played around quite a bit now, and can't seem to get variables to
I've played around with the WPF Popup Control and as far as I can
I have played around with Scala for a while now, and I know that
I have played around and implemented JQuery cycle to create a carousel with images,
I have recently clone QuickDialog project from github and played around with it. They

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.