src/share/classes/java/util/Collections.java

Print this page
rev 6969 : [mq]: iterator

@@ -28,10 +28,11 @@
 import java.io.ObjectOutputStream;
 import java.io.IOException;
 import java.lang.reflect.Array;
 import java.util.function.BiConsumer;
 import java.util.function.BiFunction;
+import java.util.function.Consumer;
 import java.util.function.Function;
 
 /**
  * This class consists exclusively of static methods that operate on or return
  * collections.  It contains polymorphic algorithms that operate on

@@ -1083,10 +1084,14 @@
                 public boolean hasNext() {return i.hasNext();}
                 public E next()          {return i.next();}
                 public void remove() {
                     throw new UnsupportedOperationException();
                 }
+                @Override
+                public void forEachRemaining(Consumer<? super E> action) {
+                    i.forEachRemaining(action);
+                }
             };
         }
 
         public boolean add(E e) {
             throw new UnsupportedOperationException();

@@ -1261,10 +1266,15 @@
                     throw new UnsupportedOperationException();
                 }
                 public void add(E e) {
                     throw new UnsupportedOperationException();
                 }
+
+                @Override
+                public void forEachRemaining(Consumer<? super E> action) {
+                    i.forEachRemaining(action);
+                }
             };
         }
 
         public List<E> subList(int fromIndex, int toIndex) {
             return new UnmodifiableList<>(list.subList(fromIndex, toIndex));

@@ -2745,10 +2755,15 @@
 
                 public void add(E e) {
                     typeCheck(e);
                     i.add(e);
                 }
+
+                @Override
+                public void forEachRemaining(Consumer<? super E> action) {
+                    i.forEachRemaining(action);
+                }
             };
         }
 
         public List<E> subList(int fromIndex, int toIndex) {
             return new CheckedList<>(list.subList(fromIndex, toIndex), type);

@@ -3274,10 +3289,14 @@
             = new EmptyIterator<>();
 
         public boolean hasNext() { return false; }
         public E next() { throw new NoSuchElementException(); }
         public void remove() { throw new IllegalStateException(); }
+        @Override
+        public void forEachRemaining(Consumer<? super E> action) {
+            Objects.requireNonNull(action);
+        }
     }
 
     /**
      * Returns a list iterator that has no elements.  More precisely,
      *

@@ -3745,10 +3764,18 @@
                 throw new NoSuchElementException();
             }
             public void remove() {
                 throw new UnsupportedOperationException();
             }
+            @Override
+            public void forEachRemaining(Consumer<? super E> action) {
+                Objects.requireNonNull(action);
+                if (hasNext) {
+                    action.accept(e);
+                    hasNext = false;
+                }
+            }
         };
     }
 
     /**
      * @serial include