Java LinkedList iterator being exhausted prematurely

Posted by Sujeet on Stack Overflow See other posts from Stack Overflow or by Sujeet
Published on 2010-12-29T10:49:32Z Indexed on 2011/01/02 0:54 UTC
Read the original article Hit count: 133

Filed under:
|
|

I am using LinkedList and retrieving an Iterator object by using list.iterator(). After that, I am checking it.hasNext(), real issue is while checking it.hasNext(), sometimes it returns false. I need help why this is happening, though I have elements in the list.

Some code:

public synchronized void check(Object obj) throws Exception {
    Iterator itr = list.iterator();

    while(itr.hasNext()) { //This Line I get false.. though i have list size is 1

        Item p = (Item)itr.next();
        if(p.getId() == null) {continue;}
        if(p.getId().getElemntId() == obj.getId() || obj.getId() == 0 ) {
            p.setResponse(obj);
            notifyAll();
            return;
        }
    }
    Log.Error("validate failed obj.getId="+obj.getId()+" **list.size="+list.size()*This shows 1*);
    throw new Exception("InvalidData");
}

© Stack Overflow or respective owner

Related posts about java

Related posts about collections