Java LinkedList iterator being exhausted prematurely
- by Sujeet
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");
}