ali abdoli
ali abdoli

Reputation: 33

How to compare values in Hastable?

I want to compare values of Hashtable:

Hashset ht = new Hashtable<Integer, Hashset>();
for (Integer i: ht.keySet()) {
  for (Integer j: ht.keySet()){
           if(ht.get(i).contain(ht.get(j)))
           {
             //do something
           }
       }
 }

I used this code and i got the error java.util.ConcurrentModificationException on the second loop. I want to check that the hashsets in my hashatable have same elements or not. How can i do it? Thanks.

Upvotes: 0

Views: 79

Answers (1)

Mikescher
Mikescher

Reputation: 893

Your problem is what you "do" inside your if.
ConcurrentModification means you are trying to change the HashSet while looping through the values by foreach. And that is not allowed

Perhaps you should think about using an ArrayList instead of an Hashtable (depends on the rest of your code ...)

In this case you could remember the "pairs" and do whatever you want to do with them later.

Upvotes: 1

Related Questions