hg: lambda/lambda/jdk: Streams cleanups; add optimized forEach implementations; include subList in tested lists
Remi Forax
forax at univ-mlv.fr
Sun Nov 18 11:57:49 PST 2012
On 11/18/2012 07:57 PM, brian.goetz at oracle.com wrote:
> Changeset: f7c32272e02f
> Author: briangoetz
> Date: 2012-11-18 13:56 -0500
> URL: http://hg.openjdk.java.net/lambda/lambda/jdk/rev/f7c32272e02f
>
> Streams cleanups; add optimized forEach implementations; include subList in tested lists
>
> ! src/share/classes/java/util/ArrayList.java
> ! src/share/classes/java/util/Arrays.java
> ! src/share/classes/java/util/Vector.java
> ! src/share/classes/java/util/stream/Spliterator.java
> ! src/share/classes/java/util/stream/Streams.java
> ! test-ng/tests/org/openjdk/tests/java/util/stream/StreamTestDataProvider.java
>
>
Brian,
the code of ArrayList.forEach should be:
Object[] elementData = this.elementData;
int size = this.size;
if (size > elementData.length)
throw new ConcurrentModificationException();
int modCount = this.modCount;
for (int i=0; i<size; i++){
if (modCount != this.modCount) {
throw new ConcurrentModificationException();
}
block.accept((E)elementData[i]);
}
You still need to throw a ConcurrentModificationException if the lambda
block
modifies the ArrayList.
A code like this should throw a CME and not an AIOOBE
arraylist.forEach(e -> { arraylist.remove(e); })
The modCount check will be hoisted by the VM if it can prove that the block
doesn't change modCount.
Also most of the implementations of java.util that uses array, tend to
do the cast
when the objects are pull out of the array instead of casting the array
to E[].
Rémi
More information about the lambda-dev
mailing list