Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Concurrency: why this snippet throws a ConcurrentModificationException?

 
Thiago Medeiros
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in the chapter Concurrency of OCP study guide, and I saw this snipped:



This throws a ConcurrentModificationException.
Changing the instance type from HashMap to ConcurrentHashMap fix the problem, but it was not clear enough for me why the snippet throws that exception.
Any explanation will be welcome

Thanks in advance
 
Junilu Lacar
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google is your friend in these situations: HashMap ConcurrentModificationException
 
sohail hussain
Ranch Hand
Posts: 73
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thiago Medeiros wrote:I'm in the chapter Concurrency of OCP study guide, and I saw this snipped:



This throws a ConcurrentModificationException.
Changing the instance type from HashMap to ConcurrentHashMap fix the problem, but it was not clear enough for me why the snippet throws that exception.
Any explanation will be welcome

Thanks in advance

you can use basic for loop that does not throw exception any hope it helps
 
Stephan van Hulst
Saloon Keeper
Posts: 10782
230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by basic for-loop you mean using an index, that's generally not the appropriate solution. The basic for loop should be avoided unless you need to operate on multiple arrays at the same time.

If you really must modify a collection while you're iterating over it (as opposed to say, modifying a copy of the collection and replacing the original with the copy after the loop is finished), then use an Iterator. The loop in the original post can be written like this:
 
Piet Souris
Bartender
Posts: 3611
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since keyset() is a view of the keys in a map, it can be a little shorter:
 
No holds barred. And no bars holed. Except this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!