ConcurrentModificationException

D

Damo

Hi,
I'm trying to merge some Lists, I've got a method called
mergeAndSort(List1, List2); I'm calling it twice.
as in
finalList = mergeAndSort(List1, List2);
finalList = mergeAndSort(finalList, List3);

When I do this I get a ConcurrentModificationException.
Does anyone know what causes this. I believe it is caused by modifying
a list while iterating through it?
 
J

John Ersatznom

Damo said:
Hi,
I'm trying to merge some Lists, I've got a method called
mergeAndSort(List1, List2); I'm calling it twice.
as in
finalList = mergeAndSort(List1, List2);
finalList = mergeAndSort(finalList, List3);

When I do this I get a ConcurrentModificationException.
Does anyone know what causes this. I believe it is caused by modifying
a list while iterating through it?

The problem has to be in your mergeAndSort method, whose code you didn't
supply. If you alter a list other than via the iterator, and then keep
using the iterator, this exception will tend to get thrown. Use the
iterator's own methods; use the ListIterator from the "listIterator"
method if you need to insert or mutate elements and not just remove
some. Or don't use iterators at all. Be sure your code is threadsafe, too.

Consider also just using an existing SortedFoo collection instead of
rolling your own.
 
D

Damo

The code for mergeAndSort is quite messy . It looks like this

private List<Result> mergeAndSort(List list, List otherList,String
engine)
{
Collection temp = new ArrayList(list);
Collection temporary = new ArrayList(otherList);

for(Iterator li = list.iterator(); li.hasNext();)
{

Result res = (Result)li.next();
String url = res.getURL();
String t = res.getTitle();
int position = res.getPosition();
for(Iterator y = otherList.iterator(); y.hasNext();)
{
Result result = (Result)y.next();
String URL = result.getURL();
String tit = result.getTitle();
int pos = result.getPosition();
if(URL.equals(url)||tit.equals(t))
{
res.setFoundon(engine);
res.setPosition(Math.min(position,pos));
otherList.remove(result);
// y.remove();
break;
}
}
mergedResults.add(res);
}

for(Iterator li = otherList.iterator(); li.hasNext();)
{
Result res = (Result)li.next();
mergedResults.add(res);
}

Collections.sort(mergedResults);
return mergedResults;
}


But even when I change the line otherList.remove(result) to
y.remove(result) .I still get the exception. It has me stumped.
 
D

Daniel Pitts

Damo said:
The code for mergeAndSort is quite messy . It looks like this
[snip]

But even when I change the line otherList.remove(result) to
y.remove(result) .I still get the exception. It has me stumped.

Yikes thats ugly!!!

How about using a Set<Result> or SortedSet<Result> instead?

That way you can "addAll" many times, and it will be faster, and it
will WORK without adding so much ugly code.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Damo schreef:
The code for mergeAndSort is quite messy . It looks like this

private List<Result> mergeAndSort(List list, List otherList,String
engine)
{
Collection temp = new ArrayList(list);
Collection temporary = new ArrayList(otherList);

for(Iterator li = list.iterator(); li.hasNext();)
{

Result res = (Result)li.next();
String url = res.getURL();
String t = res.getTitle();
int position = res.getPosition();
for(Iterator y = otherList.iterator(); y.hasNext();)
{
Result result = (Result)y.next();
String URL = result.getURL();
String tit = result.getTitle();
int pos = result.getPosition();
if(URL.equals(url)||tit.equals(t))
{
res.setFoundon(engine);
res.setPosition(Math.min(position,pos));
otherList.remove(result);

This line is the problem. You do not use the iterator to remove the result.

That said, there are really better tricks to keep variables apart
isntead of short and long forms. How about just using res1 and res2?
Much less confusing, if you ask me. Furthermore, you are mixing up
generic and non-generic lists. If you are sure list and otherList
contain Results, then why not declare them as such? And you do not use
the two temporary collections.

Seems like you want to compute the difference of two lists. It would
probably be better to implement a suitale equals() method in Result and
just use list.removeAll(otherList).

Last of all, you are probably better off writing a Comparator<Result>
and using Collections.sort(list).

Furthermore, the signature of this method does not fit what you wrote in
your first post. Do not waste our time here by posting fake and
incompilable code.

H.
// y.remove();
break;
}
}
mergedResults.add(res);
}

for(Iterator li = otherList.iterator(); li.hasNext();)
{
Result res = (Result)li.next();
mergedResults.add(res);
}

Collections.sort(mergedResults);
return mergedResults;
}


But even when I change the line otherList.remove(result) to
y.remove(result) .I still get the exception. It has me stumped.


- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFr1vre+7xMGD3itQRAqRDAKCCTyzMQYi2kyLgDqKoUhDK2YKq/wCeN3QD
9W/fYMwkV15DjXfjzPLy8bk=
=AFrA
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top