Suggest an editImprove this articleRefine the answer for “What is the purpose of the Iterator pattern?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The purpose of the **Iterator** pattern is to **provide a single way to sequentially traverse the elements of a collection**, **without exposing its internal structure**. **Key point:** the Iterator pattern makes collections convenient and universal to use, letting you traverse them without knowing their internal structure.Shown above the full answer for quick recall.Answer (EN)ImageThe purpose of the **Iterator** pattern is to **provide a single way to sequentially traverse the elements of a collection**, **without exposing its internal structure**. --- ### 1. **The Core Idea** Instead of the client working directly with arrays, lists, or trees, the iterator takes over the logic of moving from one element to the next. The client doesn't need to know: - how the collection is structured, - how it stores its data, - what the traversal logic is (in order, in depth, by filter, and so on). It simply uses a **single interface**: ```java Iterator iterator = collection.iterator(); while (iterator.hasNext()) { Object item = iterator.next(); } ``` --- ### 2. **Why It's Needed** - To **separate the traversal algorithm** from the collection's structure. - To **unify access** to elements of different containers (lists, trees, hash maps). - To **support multiple traversal methods** without duplicating logic. --- ### 3. **Advantages** - Encapsulation of the collection's internal structure. - The ability to create **different types of iterators** (forward, reverse, filtering). - Simplified client code, traversal always looks the same. --- ### 4. **Example (Java)** ```java List<String> list = List.of("A", "B", "C"); Iterator<String> it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } ``` The client doesn't know that an `ArrayList` is backed by an array internally; it just fetches elements sequentially. --- ### **Conclusion** The **Iterator** pattern makes collections **convenient and universal to use**, letting you traverse them **without knowing their internal structure** and providing a **single navigation interface** over the elements.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.