8005051: optimized defaults for Iterator.forEachRemaining
Reviewed-by: alanb, mduigou, psandoz, ulfzibis
Contributed-by: Akhil Arora <akhil.arora@oracle.com>
--- a/jdk/src/share/classes/java/util/ArrayList.java Thu Apr 25 17:10:36 2013 -0700
+++ b/jdk/src/share/classes/java/util/ArrayList.java Tue Apr 23 11:54:22 2013 -0700
@@ -29,10 +29,6 @@
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
-import java.util.function.Consumer;
-import java.util.function.Predicate;
-import java.util.function.UnaryOperator;
-
/**
* Resizable-array implementation of the <tt>List</tt> interface. Implements
* all optional list operations, and permits all elements, including
--- a/jdk/src/share/classes/java/util/LinkedList.java Thu Apr 25 17:10:36 2013 -0700
+++ b/jdk/src/share/classes/java/util/LinkedList.java Tue Apr 23 11:54:22 2013 -0700
@@ -957,6 +957,7 @@
next = next.next;
nextIndex++;
}
+ lastReturned = next;
checkForComodification();
}
--- a/jdk/src/share/classes/java/util/Vector.java Thu Apr 25 17:10:36 2013 -0700
+++ b/jdk/src/share/classes/java/util/Vector.java Tue Apr 23 11:54:22 2013 -0700
@@ -29,8 +29,6 @@
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
-import java.util.function.Consumer;
-
/**
* The {@code Vector} class implements a growable array of
* objects. Like an array, it contains components that can be
@@ -1161,7 +1159,7 @@
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
synchronized (Vector.this) {
- final int size = Vector.this.elementCount;
+ final int size = elementCount;
int i = cursor;
if (i >= size) {
return;
--- a/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java Thu Apr 25 17:10:36 2013 -0700
+++ b/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java Tue Apr 23 11:54:22 2013 -0700
@@ -1059,6 +1059,17 @@
public void add(E e) {
throw new UnsupportedOperationException();
}
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public void forEachRemaining(Consumer<? super E> action) {
+ Objects.requireNonNull(action);
+ final int size = snapshot.length;
+ for (int i=cursor; i < size; i++) {
+ action.accept((E) snapshot[i]);
+ }
+ cursor = size;
+ }
}
/**
@@ -1367,6 +1378,15 @@
public void add(E e) {
throw new UnsupportedOperationException();
}
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public void forEachRemaining(Consumer<? super E> action) {
+ Objects.requireNonNull(action);
+ while (nextIndex() < size) {
+ action.accept(it.next());
+ }
+ }
}
// Support for resetting lock while deserializing