For my application, element access is expensive, so java.util.Iterator is no use. I want something more like C++ iterators, where I can move the pointer around without returning an element. Is there something in the standard library like this, or has some de facto standard interface evolved through custom? (If not, please don't waste your time posting code snippets - I'm quite able to think up reasonable names which will do the job).
Is there a conventional iterator interface in Java which separates traversal from element access?
144 Views Asked by fizzer At
2
There are 2 best solutions below
9

java.util.Iterator is an interface, not an implementation. The behaviour of your iterator will depend on the collection you are using and how you obtained it.
Most of them should not be doing anything expensive as, for Objects, Java passes around their reference. Whatever you're accessing that is expensive should be abstracted away behind an Object that allows you to refer to it without trying to access it.
If your gripe is instead with the interface and a desire for other methods, then you'll need to come up with and conform to that interface by yourself; java.util.Iterator is the conventional iterator for Java, in part likely for the reasons I've mentioned.
(Since no one came around to write a proper answer, I'll expand my comments into an answer.)
To the extent of my knowledge there's no interface in the standard API that suits you (i.e. an interface with something like
iterator.skip()
). The best solution using standard API methods I believe, would be to doyourList.sublist(startIndex).iterator()
for instance.I think that, if possible, you should consider creating your own iterator interface. (Note that you can let it extend the
java.util.Iterator
interface to be able to use it as an ordinary iterator if needed.)Finally, and this is mostly for future readers that have to use
java.util.Iterator
and have to access the underlying list lazily, you can use ajava.lang.reflect.Proxy
. Note thatProxy
objects doesn't always play well with serialization / persistence libraries, and it requires that you work with interfaces. An example follows:Here's a slow
OnDiskList
that fetches line n in each call toget(int n)
:Here's how the output looks when using an ordinary iterator:
Output:
Now to the magic: Here's a
ProxyingIterator
that hides the object behind aProxy
that accesses the list lazily:When using this iterator it looks as follows:
Output: