Nested iterators (well, not nested exactly...)

R

Russ Perry Jr

Is it safe to "nest" iterators? In other words, if I'm iterating over
a Vector, and in the middle of doing that (specifically after removing
an element) I iterate over the same Vector to find something out in
the middle if the iterator loop, is that okay?

It goes something like this:


private Vector listOfSessions; // vector of SessionObjects


public boolean isLoginNameStillOnList(String loginNameToCheck)
{
boolean result = false;

for (Iterator iter = listOfSessions.iterator();
iter.hasNext();)
{
if (((SessionObject) iter.next()).getLoginName().
equalsIgnoreCase(loginNameToCheck))
{
result = true;
break;
}
}

return result;
}


public boolean removeSessionFromList(String sessionID)
{
boolean result = false;

for (Iterator iter = listOfSessions.iterator();
iter.hasNext();)
{
SessionObject sessionObject = (SessionObject) iter.next();

if (sessionObject.getSessionID().equalsIgnoreCase(sessionID))
{
/* ignore return value */ iter.remove();

// check if there are no other instances of loginName
// in the list...
if (!isLoginNameStillOnList(sessionObject.getLoginName()))
{
// ...if not, log them out of database entirely
logoutFromDatabase(sessionObject);
}

result = true;
break;
}
}

return result;
}
 
J

John C. Bollinger

Russ said:
Is it safe to "nest" iterators? In other words, if I'm iterating over
a Vector, and in the middle of doing that (specifically after removing
an element) I iterate over the same Vector to find something out in
the middle if the iterator loop, is that okay?

Yes, as long as you don't modify the Vector during any iteration except
via the corresponding Iterator. If you have two Iterators active over
the same Collection at the same time then modifying the collection via
either one will require that you stop using the other. That doesn't
appear to be a problem in your code.


John Bollinger
(e-mail address removed)
 
R

Russ Perry Jr

Yes, as long as you don't modify the Vector during any iteration except
via the corresponding Iterator. If you have two Iterators active over
the same Collection at the same time then modifying the collection via
either one will require that you stop using the other. That doesn't
appear to be a problem in your code.

Okay, cool.

I think we'll probably end up changing it anyway because a post in
another thread -- I think by you as well -- got me thinking about
threads and synchronization...

But thanks very much for confirming this; it wasn't evident from
the javadoc if the Vector iterator() returns unique iterators or
the same one (I suppose it would be a class method rather than an
object method if so, but better safe than sorry).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top