Can we write our own iterator in Java?

Sure. An iterator is just an implementation of the java.util.Iterator interface. If you’re using an existing iterable object (say, a LinkedList) from java.util, you’ll need to either subclass it and override its iterator function so that you return your own, or provide a means of wrapping a standard iterator in your special Iterator instance (which has the advantage of being more broadly used), etc.

Leave a Comment