Interface ClosableIterator<T>

  • Type Parameters:
    T - The element type
    All Superinterfaces:
    AutoCloseable, Iterator<T>
    All Known Implementing Classes:
    LockIterator

    public interface ClosableIterator<T>
    extends Iterator<T>, AutoCloseable
    Iterator holding a resource that must be free'd by the consumer.
    Close is an idempotent function and can be performed multiple times without effects beyond first invocation.

    This closes automatically when Iterator.hasNext() returns false but its recommended to only be used within a try-with-resources block for safety.

    Example

    This can handle any exceptions thrown while iterating and ensures the lock is released correctly.
    
     try (ClosableIterator<T> it = cacheView.lockedIterator()) {
         while (it.hasNext()) {
             consume(it.next());
         }
     }
     
    Since:
    4.0.0