Browse Java Design Patterns & Enterprise Application Architecture

Enhancing Iterators

Extend Java iterators carefully when traversal needs filtering, peeking, or domain-specific movement without turning the cursor API into a grab bag.

On this page

Custom iterator enhancements can be useful, but they should remain coherent.

Useful Enhancements

  • peeking
  • filtered iteration
  • bidirectional movement
  • domain-specific step sizes or traversal modes

Risks

  • bloated cursor APIs
  • hidden expensive operations
  • unclear interaction with mutation

Review Rule

Enhance iterators only when the traversal contract genuinely needs richer cursor semantics. Otherwise, a stream, query object, or separate traversal service may be clearer.

Revised on Thursday, April 23, 2026