1

My code (cut down to bare essentials is below); it's throwing a ConcurrentModificationException when the hasNext() is called the last time- PS- I've tried debugging it and I cannot seem to determine how to make it work. If I switch the remove and add methods, it works, but it removes the only value if somethingElse = something which isn't really helpful.

PS- I know of the solution here; but I need it to add AFTER removing the value which is causing the problem

Iterator<String> itr = something.iterator();
while (itr.hasNext()){
    String x = itr.next();
    itr.remove();
    somethingElse.add(x);            
}

Edit- somethingElse can be something or a new object all-together (there's a loop that determines this) This is my first post- if it's too abstract or if the code isn't clear, let me know and I'll post my full code.

Thanks! :)

Community
  • 1
  • 1
JsDart
  • 183
  • 13
  • 1
    Are you sure `something` and `somethingElse` are two unrelated objects? – Codebender Feb 03 '16 at 05:52
  • can you clarify what is `somethingElse`. Every operation that happens in loop must be through iterator. If iteration is not used that throws the `ConcurrentModificationException` – Vivek Singh Feb 03 '16 at 05:53
  • somethingElse can be something (there's a loop that determines whether somethingElse is something or a new object) – JsDart Feb 03 '16 at 05:53
  • You cannot add to the collection while using an iterator on it... I would suggest adding to a new object and then copying after the iteration is complete... – Codebender Feb 03 '16 at 06:03
  • Thanks for the reply! I'll try it out and get back, albeit it is odd that it works initially (when somethingElse is a new object and something) until the last value is left to process. After it processes the last value and checks for hasNext(), it spits out the error. I'll create a map and do this and post back if it works though, thanks! :) – JsDart Feb 03 '16 at 06:14

2 Answers2

0

What is understood by your question- any point of time SomethingElse can be something.

If that's the case you will get concurrentModificationException as you are iterating over the list and at the same time you are updating it.

So iterator has no clue what is going on in the underlying data structure it will stick to it's policy of fail-fast before something goes horribly wrong.

(Core value followed by JDK : Read and write should be handled gracefully)

Helios
  • 851
  • 2
  • 7
  • 22
  • That makes sense, thank you for explaining that. However, swapping the order of removing and adding fixes the error; can you kindly explain me why that is before I mark the question solved? – JsDart Feb 03 '16 at 19:33
  • Swapping order doesn't make any difference. Might be when you ran after swapping that `someThingElse` was really a new list which satisfied `JVM` and it worked for you but in all other cases it will fail. – Helios Feb 04 '16 at 07:59
0

Your .remove is throwing that error because you are modifying the list while iterating thorugh it. You can use ConcurrentHashMap and CopyOnWriteArrayList classes to fix this error.

Refer to the link here for clear cut explanation about the error.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78