• Post Reply Bookmark Topic Watch Topic
  • New Topic

How To Get Rid Of java.util.ConcurrentModificationException?  RSS feed

 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I have written a code and the as the thread title mentions that I am getting this exception. The thing is my code requirement is such that I have to update a Hashtable and while iterating on it. Could you please tell me how to solve this?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you change the content of a collection like a Hashtable while iterating it, you'll get a ConcurrentModificationException, because the iterator can't deal with a collection that's changing while it is iterating over it. Change your code so that you don't modify the Hashtable while iterating over it. For example, store the changes in a second collection temporarily.

There's one exception: when you need to remove entries from a collection while iterating it, you can call remove() on the Iterator (not on the collection) to remove the element that the iterator is currently pointing to.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, certain modifications are allowed. (1) You can remove elements using the iterator methods, and (2) you can change the elements themselves (assuming that they are mutable).

For everything else, you'll will need to do something else. Can you give us an example of what you are trying to do?

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:(2) you can change the elements themselves (assuming that they are mutable).

Do not change the content of objects that are in a hash-based collection such as a Hashtable, HashMap or HashSet! If you change the content of an object that's inside a hash-based collection so that its identity changes (oldobject.equals(newobject) is false, which means oldobject.hashCode() != newobject.hashCode()), the hash-based collection will get in an inconsistent state and you will get strange and hard to track bugs.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:Do not change the content of objects that are in a hash-based collection such as a Hashtable, HashMap or HashSet!

That applies to the map keys (and to hashset elements) only. You can safely change the map values.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your reply. I solved it by storing the data in some other temporary Hashtable.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, why are you using Hashtable instead of HashMap?

Hashtable is a legacy collection class from Java 1.0 / 1.1. Since Java 1.2, the newer collection classes were added, including HashMap, which isn't unnecessarily synchronized and is therefore more efficient than Hashtable.

Prefer to use the new collection classes (for example HashMap, ArrayList) above the old ones (Hashtable, Vector).
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion Jesper. Will change the Hashtables with HashMaps!
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also would like to know that is there any collection class that can store data in sets of threes, instead of the normal <K, V>? Is there anyway to get <K, V, V> form in a collection? Actually i have to store three sets of values lets say A, B and C. So i had to use two HashMaps, one with <A, C> and one <B, C>.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have three items, A, B and C. What exactly do you want to do with those items - which of them are keys, and which are values? First you mention <K, V, V> which suggests that A is the key and B and C are values under that key. But then you mention you have to Maps with <A, C> and <B, C> - which means, A and B are keys to look up item C. That's something different than you suggested first.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusing post. A, B and C are three items. In one HashMap <A, C> A is the key and C is the value. In the second <B, C>, B is the key and C is the value. So instead of having two HasMaps i wanted to know that can i have one collection in which i can store <A, B, C> together making any one of them key.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. What if you would make a class that contains items A and B, and use that as the key?

And then create a HashMap<D, C>.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!