Interface ClosableIterator<T>

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

    public interface ClosableIterator<T>
    extends java.util.Iterator<T>, java.lang.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
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void close()  
      • Methods inherited from interface java.util.Iterator

        forEachRemaining, hasNext, next, remove
    • Method Detail

      • close

        void close()
        Specified by:
        close in interface java.lang.AutoCloseable