Skip to main content

Improvements to List mutating operations

You can perform two improvements.

If ArrayList size is known ahead of time capacity should be specified when the object is created
l = new ArrayList<>(100);


ArrayList.remove() has linear O(n) performance.  Elements after the index have to be shifted up.  To avoid the overhead use LinkedList

private static List<Integer> l;
private static void removeA() {
    l = new LinkedList<>();
    IntStream.range(0, 100).forEach((i) -> l.add(i));
    l.remove(10);
}

Comments