Class UnmodifiableIterator<E>

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

    public class UnmodifiableIterator<E>
    extends ReadOnlyIterator<E>
    An Iterator that is unmodifiable, meaning the elements cannot be removed. This class extends the ReadOnlyIterator, which throws an exception when the remove operation is attempted.

    Example Usage

       List<String> list = Arrays.asList("one", "two", "three");
       Iterator<String> unmodifiableIterator = new UnmodifiableIterator<>(list.iterator());
    
       while (unmodifiableIterator.hasNext()) {
           System.out.println(unmodifiableIterator.next());
       }
    
       // Attempting to remove will throw an IllegalStateException
       try {
           unmodifiableIterator.remove();
       } catch (IllegalStateException e) {
           System.out.println("Cannot remove elements from an unmodifiable iterator.");
       }
     
    Since:
    1.0.0
    Author:
    Mercy
    • Constructor Summary

      Constructors 
      Constructor Description
      UnmodifiableIterator​(java.util.Iterator<E> delegate)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void forEachRemaining​(java.util.function.Consumer<? super E> action)  
      boolean hasNext()  
      E next()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • UnmodifiableIterator

        public UnmodifiableIterator​(java.util.Iterator<E> delegate)
    • Method Detail

      • hasNext

        public boolean hasNext()
      • next

        public E next()
      • forEachRemaining

        public void forEachRemaining​(java.util.function.Consumer<? super E> action)