• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurrent Modifiaction Exception

 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm new to java, I'm nt bale to fix this exception Concurrent Modifiaction, I used a file to read data from it and store it on a linked list
data in my file is of the form
Slno Name
01 abc
01 def
02 abc
03 xyz
then i read it from the linked list and then store it by grouping them into intemsets using multimaps Map<String, List<String>>.

I need data to be stored like this:
01 {abc,def}
02 {abc}
03 {xyz}


Data gets stored on to the linked list but I'm unable to read it from the list and store it on to multimaps.

These are the errors I'm getting:
java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at Itemset.main(Itemset.java:41)


I am sure there is a better way to do this, Please help me and let me know how to fix this exception or a better way to do this.


Thanks in advance.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error happens when you modify a collection that you're actively iterating over; i.e., if you have a Iterator, and the collection is modified, the next time you use the Iterator, this exception can happen. If you can use a ListIterator instead of an Iterator, that class lets you delete items through the iterator, if that's what you need to do.
 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have used listiterator but still happen to get the same exception.
Please tell me how to do it.Can this be achieved using a different collection if so which should be used?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote: If you can use a ListIterator instead of an Iterator, that class lets you delete items through the iterator, if that's what you need to do.


Still you can remove/delete an items in the List by Iterator . ListIterator provides extra add() method .
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:

Still you can remove/delete an items in the List by Iterator . ListIterator provides extra add() method .


You're right, of course. Posting before coffee is never a good idea

In any case, let's see the main() method in Itemset.
 
Rob Spoor
Sheriff
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic