8155600: Performance optimization of Arrays.asList().iterator()

Reviewed-by: redestad, shade, plevart, attila
This commit is contained in:
Tagir F. Valeev 2016-04-29 16:52:05 +03:00
parent a6f2cdbb0f
commit 6db653621d
2 changed files with 123 additions and 1 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -4403,6 +4403,35 @@ public class Arrays {
public void sort(Comparator<? super E> c) {
Arrays.sort(a, c);
}
@Override
public Iterator<E> iterator() {
return new ArrayItr<>(a);
}
}
private static class ArrayItr<E> implements Iterator<E> {
private int cursor;
private final E[] a;
ArrayItr(E[] a) {
this.a = a;
}
@Override
public boolean hasNext() {
return cursor < a.length;
}
@Override
public E next() {
int i = cursor;
if (i >= a.length) {
throw new NoSuchElementException();
}
cursor = i + 1;
return a[i];
}
}
/**