Why isn't there a PeekingListIterator?

67 views
Skip to first unread message

Glenview Jeff

unread,
Mar 4, 2012, 12:19:52 PM3/4/12
to guava-...@googlegroups.com

Also posted on Stack Overflow.

I need a "peekable" ListIterator that lets me peek at the previous and next list elements without moving the cursor. This is akin to PeekingIterator, only bidirectional because PeekingIterator only has a next() method, not a previous().

Does this need to be implemented by my writing a new PeekingListIterator class or is there an Guava-intended way to combine the two concepts?  If I write the class, I'll submit a change request, but I'm just surprised it's not already part of Guava.

Glenview Jeff

unread,
Mar 4, 2012, 12:48:24 PM3/4/12
to guava-...@googlegroups.com
Also, I'm not sure if this is a bug or by design, but in trying to figure out how this PeekableIterator works, I wrote the following test.  It appears that peekingIterator doesn't copy the iterator passed in the parameter.  Instead it appears to keep a handle on the passed iterator.  When peek() is called, it appears to be internally advancing to next() without going back to previous(), causing the following test to fail:

final ListIterator<String> listIterator = flavors.listIterator();
final PeekingIterator<String> peekingIterator = Iterators.peekingIterator(listIterator);
assertEquals("chocolate", peekingIterator.peek());
assertEquals("chocolate", listIterator.next()); // Fails, is vanilla.

Kevin Bourrillion

unread,
Mar 4, 2012, 1:01:36 PM3/4/12
to Glenview Jeff, guava-...@googlegroups.com
Answered original question on SO.


On Sun, Mar 4, 2012 at 9:48 AM, Glenview Jeff <glenvi...@gmail.com> wrote:
Also, I'm not sure if this is a bug or by design, but in trying to figure out how this PeekableIterator works, I wrote the following test.  It appears that peekingIterator doesn't copy the iterator passed in the parameter. 

You can't really copy an iterator, just create some collection and slurp the entire contents in, which would be pretty evil in a method that didn't make it very clear it was going to do that.

This is why javadoc says that the returned PeekingIterator will be "backed by" the input iterator.

 
Instead it appears to keep a handle on the passed iterator.  When peek() is called, it appears to be internally advancing to next() without going back to previous(), causing the following test to fail:

final ListIterator<String> listIterator = flavors.listIterator();
final PeekingIterator<String> peekingIterator = Iterators.peekingIterator(listIterator);
assertEquals("chocolate", peekingIterator.peek());
assertEquals("chocolate", listIterator.next()); // Fails, is vanilla.

Parameters:
iterator - the backing iterator. The PeekingIterator assumes ownership of this iterator, so users should cease making direct calls to it after calling this method.

You should show SO what you're really trying to do and just ask what the best way to code it is.  I somewhat doubt that the best way will really rely on "peeking," but I could be wrong.



--
Kevin Bourrillion @ Google
Java Core Libraries Team
http://guava-libraries.googlecode.com

Reply all
Reply to author
Forward
0 new messages