Which Java ArrayList Traversal Is Fastest? Benchmarks and Best Practices
This article explains Java's ArrayList structure, compares four traversal methods with benchmark code, analyzes their performance across different list sizes, and outlines safe deletion techniques and common pitfalls such as subList casting and concurrency issues.
ArrayList is a dynamic array implementation in Java 1.8, extending AbstractList and implementing List, RandomAccess, Cloneable, and Serializable. It stores elements in an internal Object[] called elementData and tracks the number of elements with size.
ArrayList data structure
elementDatais a dynamic array that grows by about 50% when capacity is exceeded; size represents the actual number of stored elements.
Traversal methods
Four common ways to iterate over an ArrayList:
Random access using a traditional for loop with index.
Using an Iterator.
Enhanced for‑each loop. forEach method with a lambda expression.
// Random access
for (int i = 0; i < size; i++) {
value = list.get(i);
}
// Enhanced for
for (String s : list) {
value = s;
}
// Iterator
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
value = iter.next();
}
// forEach with lambda
list.forEach(p -> {
p.hashCode();
});Performance test
The following test code measures the execution time of each traversal method for different list sizes.
public class ArrayListTest {
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(10, 50, 100, 500, 1000, 10000,
50000, 100000, 5000000, 10000000, 30000000);
for (Integer i : integers) {
testRand(i);
}
}
// methods testRand, testRandFor, testFor, testIterator, testForEach, createTestList ...
}Results show that for small data sets all four loops have similar cost, but as the size grows the forEach loop becomes the most efficient, while the traditional for loop is the slowest. An initial warm‑up effect makes the first forEach measurement unusually high.
Deletion techniques
Correct ways to delete elements from an ArrayList are:
Using an Iterator 's remove() method (recommended by Alibaba code guidelines).
Iterating backwards and removing by index.
// Iterator deletion
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
iter.next();
iter.remove();
}
// Reverse loop deletion
for (int i = list.size() - 1; i >= 0; i--) {
list.remove(i);
}Incorrect approaches include deleting with a forward for loop (which skips elements) and using the enhanced for loop, which throws ConcurrentModificationException.
Common pitfalls
Do not cast the result of subList() to ArrayList; it returns a view ( ArrayList$SubList) and casting causes ClassCastException.
Modifying the original list while iterating a subList can trigger ConcurrentModificationException.
Specify an initial capacity for an ArrayList to reduce resizing overhead. ArrayList is not thread‑safe.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
