Class EmptyIterator<E>

  • Type Parameters:
    E - the type of elements returned by this iterator
    All Implemented Interfaces:
    java.util.Iterator<E>

    @Immutable
    public class EmptyIterator<E>
    extends java.lang.Object
    implements java.util.Iterator<E>
    An empty and immutable implementation of the Iterator interface.

    This class provides a singleton instance through the public static field INSTANCE, which can be used directly without creating additional instances. It is safe for use in multi-threaded environments since it does not maintain any mutable state.

    Example Usage

    
     // Using the singleton instance
     Iterator<String> iterator = EmptyIterator.INSTANCE;
    
     // Iterating over the empty iterator
     while (iterator.hasNext()) {
         String next = iterator.next(); // Will never enter the loop
     }
    
     // Attempting to remove from the iterator will throw an exception
     try {
         iterator.remove();
     } catch (UnsupportedOperationException e) {
         System.out.println("Cannot modify an empty, immutable iterator.");
     }
     
    Author:
    Mercy
    See Also:
    Iterator, Collections.emptyIterator()
    • Constructor Summary

      Constructors 
      Constructor Description
      EmptyIterator()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean hasNext()  
      E next()  
      void remove()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Constructor Detail

      • EmptyIterator

        public EmptyIterator()
    • Method Detail

      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<E>
      • next

        public E next()
        Specified by:
        next in interface java.util.Iterator<E>
      • remove

        public void remove()
        Specified by:
        remove in interface java.util.Iterator<E>