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?
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.
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 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.
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).
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>.
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.
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.