8202685: Optimize ArrayList subList replaceAll

Reviewed-by: martin, psandoz, igerasim, redestad, dholmes, smarks, jrose, plevart
This commit is contained in:
Doug Lea 2018-05-22 21:46:51 -07:00
parent c9e23b5e71
commit e4046542ba
2 changed files with 58 additions and 12 deletions

View file

@ -1221,6 +1221,10 @@ public class ArrayList<E> extends AbstractList<E>
return true;
}
public void replaceAll(UnaryOperator<E> operator) {
root.replaceAllRange(operator, offset, offset + size);
}
public boolean removeAll(Collection<?> c) {
return batchRemove(c, false);
}
@ -1724,15 +1728,18 @@ public class ArrayList<E> extends AbstractList<E>
@Override
public void replaceAll(UnaryOperator<E> operator) {
replaceAllRange(operator, 0, size);
modCount++;
}
private void replaceAllRange(UnaryOperator<E> operator, int i, int end) {
Objects.requireNonNull(operator);
final int expectedModCount = modCount;
final Object[] es = elementData;
final int size = this.size;
for (int i = 0; modCount == expectedModCount && i < size; i++)
for (; modCount == expectedModCount && i < end; i++)
es[i] = operator.apply(elementAt(es, i));
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
modCount++;
}
@Override