my dog learned polymorphism
The moose likes Java in General and the fly likes Problem with Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with Iterator" Watch "Problem with Iterator" New topic

Problem with Iterator

amitesh kumar
Ranch Hand

Joined: Aug 01, 2006
Posts: 50

In the above code snippet, a set of ids are present in the padviceSet in as strings. Now Using Iterator i'm trying to create a list of Advices (an object). The problem I am facing is that when I am debuging my code in Eclipse the insert() method is not called and no desired output is resulted. But on direct running the code thru web browser the "pas" is set properly, insert is called and desired output generated.

I doubt there is some problem with the iteration logic. Please help me. Also, I get NoSuchElementException on call.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

It sounds to me as if the program is multithreaded, and some other thread is removing items from padviceIdSet as you're trying to iterate over it (although I'd expect a ConcurrentModificationException in that case -- but perhaps the failsafe mechanism is... failing.) I don't see any kind of synchronization here.

In any case, this would explain how drastic timing changes could cause a difference.

[Jess in Action][AskingGoodQuestions]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Yes, another thread removing items does seem the most likely cause of the problem here. You might also take a look at this "StringSet", which is not a standard library class. It could be that the iterator() method here is returning a buggy implementation. Might be worth a look, especially if you're not using multiple threads in your program.

This could also be why you're not seeing a ConcurrentModificationException - the StringSet code may not be designed to throw one. Note that this is not a bug per se, as throwing ConcurrentModificationException is not part of the responsibilities of any Collection interface. It's just that many specific implementations do throw CME (or try to, in most cases). But some don't, and this is perfectly acceptable.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Problem with Iterator
It's not a secret anymore!