Iterator pattern 00:00:00 Make a prediction You will separate traversal state from a collection and explain what happens at exhaustion or concurrent mutation. Two readers traverse the same three-book list. One has advanced to book two. If the cursor lives on the list itself, what book does the second reader see first? It may start at book two or later because both readers share the same position. Give each traversal its own iterator and define what happens when it reaches the end. 00:00:30 Noor and the place marker Two inspectors walk the same train with separate place markers. One inspector reaching carriage three does not move the other's marker. Replacing a carriage mid-inspection, however, needs an agreed rule. Each iterator owns its traversal position. The collection decides whether mutation during traversal is forbidden, reflected live or hidden by a snapshot. The Iterator pattern is a behavioral design pattern. A collection creates an iterator, an object that holds the position of one walk and offers two operations: is there more, and give me the next element. Without it, every client contains knowledge about storage, and storage can not change any more. Two walks at the same time are impossible if the position lives in the collection. 00:01:21 Walk two lists independently A reading list contains three books. One reader advances to book two while another has just started. If position lives on the collection, one reader moves the other. Give each traversal its own iterator holding its own cursor. Both read the same collection through the iteration contract. The first reader can reach the end while the second still returns book one. Define what happens if a book is removed during traversal. Move A to its second item, then ask B for its first. Both traversals should still see their own positions. If B begins at the third item, their progress is stored on the shared collection. One cursor per walk also makes a nested loop predictable. 00:02:05 Traverse a tree through a simple interface A file browser can expose an iterator over descendants even though its directory structure branches. A depth-first iterator privately holds a stack. A client repeatedly asks for the next file and need not maintain the stack or know the representation. Follow a folder that contains two child files. The iterator keeps enough pending directories to resume its walk after each yield. The caller still asks only for the next file. If a directory disappears during the walk, choose and document whether traversal skips it or reports an error. An iterator over changing data can miss or duplicate entries unless the collection documents snapshot or mutation semantics. A plain indexed loop can be sufficient for an array when clients deliberately need positions and no independent traversal abstraction is required. Java collections. ArrayList, LinkedList, TreeSet and HashSet implement Iterable. The iterator of ArrayList is documented as fail-fast: after a structural change that did not go through the iterator, it throws ConcurrentModificationException. java.util.concurrent. Iterators of most concurrent collections are weakly consistent: they may run together with other operations and never throw that exception. CopyOnWriteArrayList offers a snapshot instead. 00:03:29 Try an interview follow-up Why not keep the current position in the collection? Then only one walk is possible at a time. Two loops, or a nested loop over the same collection, would share the position and skip elements. Each walk needs its own. A listener walks a playlist while another action removes the next track. Should traversal reflect edits, use a snapshot, or signal that the traversal is invalid? Pick one documented behavior. Then run that exact interleaving and check which track is returned. The iterator owns more than an index. 00:04:10 What to take with you Take the position out of the collection. The collection creates iterators, each iterator is one walk, and the client knows only more and next. Join the protocol of your language. Write generators for ordinary walks and a class when the position matters: a stack for trees, a heap for merging, a key for pages.