• 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

java.util.ConcurrentModificationException occurs in a Map program

 
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
this is related to one program related to collection.
Here is the code:


and here is the output:


My question is why the Exception is thrown here?

As far I understand, remove() method of Map is the cause(probably).
I rewrote the code here and the Exception is Gone!

here is the new code:


Still my understanding is not clear. Why this java.util.ConcurrentModificationException was happening and why it is not happening now?
Thanks in advance.

Regards.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is occurring because you are iterating over the maps set of keys and modifying the map at the same time. In your second attempt you copy the map and get the keyset from the copy and hence modifying the original map works. Another way of gettig this to work would be to make a copy of the set of keys and iterate over the copy or you could just use the iterator returned by the map which has a remove method.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Tony. I got it.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said in the above post, you are modifying and reading the HashMap at the same time.

If you wish to avoid this, then do not read and write at the same time.

Or, if you really want to read and write at the same time, use a ConcurrentHashMap which will allow you to both read and write at the same time, but in my experience can use slightly more CPU.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Joe.
 
Marshal
Posts: 66192
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using string tokenizer, which has been regarded as legacy code for about 13 years?
 
Joe Thatcher
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why are you using string tokenizer, which has been regarded as legacy code for about 13 years?


Could you post a source for this? Everybody I know uses Strings, and I have never had an issue with them. What is so bad about using them, and since when where they legacy?
 
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joe Thatcher wrote:

Campbell Ritchie wrote:Why are you using string tokenizer, which has been regarded as legacy code for about 13 years?


Could you post a source for this? Everybody I know uses Strings, and I have never had an issue with them. What is so bad about using them, and since when where they legacy?


From the Javadoc page of StringTokenizer itself:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.


Now String.split and the java.util.regex package have been added in Java 1.4, so I'd say that's the moment StringTokenizer became legacy code.
 
Rob Spoor
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joe Thatcher wrote:Or, if you really want to read and write at the same time, use a ConcurrentHashMap which will allow you to both read and write at the same time, but in my experience can use slightly more CPU.


If you only need to remove elements, you can also use an explicit Iterator. That's a lot cheaper than using a ConcurrentHashMap.
Sujoy, this works because keySet() doesn't "extract all the keys from the map and stores them in a set", it returns a set that is a direct view of the map. If you remove anything from this set, you remove the matching entry from the map. If you add anything to the map, you add an entry to the key set as well. (You can't add anything to the set, because you can't specify the value for the key. Trying to call add will cause an UnsupportedOperationException to be thrown.)
 
Campbell Ritchie
Marshal
Posts: 66192
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The tokeniser is legacy code, not String. I see Rob has explained that already.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for their suggestions.
 
my overalls have superpowers - they repel people who think fashion is important. 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!