I'd like to compare elements within a set, in order to merge elements too similar. In order to gain some time, I would like to erase similarities during the iteration. Here how I try to proceed:
Iterator<Personne> i1 = personnes.iterator();
while (i1.hasNext()) {
Personne personneObservee = i1.next();
Set<Personne> tmp = new HashSet<Personne>();
tmp.addAll(personnes);
Iterator<Personne> i2 = tmp.iterator();
while (i2.hasNext()) {
Personne autrePersonne = i2.next();
if (personneObservee.isSimilarTo(autrePersonne)) {
personnes.remove(autrePersonne);
}
}
result.add(personneObservee.toString());
}
As you can guess from my presence here, it doesn't work, giving me this nice stacktrace :
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:926)
at java.util.HashMap$KeyIterator.next(HashMap.java:960)
at NameDetectorWithBenefits.mergeSamePersons(NameDetectorWithBenefits.java:41)
at App.main(App.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:744)
At first, I thought i1 and i2 were iterating over the same set and that I would find an answer here. As a consequence, I create a temporary set each time. However, it didn't resolve the problem.
Any ideas where the trouble might come from ?