List
A List is an ordered collection that allows duplicates and positional (indexed) access. Use ArrayList by default; LinkedList only for rare cases where you insert/remove at both ends frequently.
Common operations
List<String> list = new ArrayList<>();
list.add("Ada");
list.add("Grace");
list.add(0, "Alan"); // insert at index 0
list.get(1); // "Ada"
list.set(1, "Ada Lovelace");
list.remove("Grace");
list.size(); // 2
list.indexOf("Ada Lovelace"); // 1
Immutable lists
List<Integer> ints = List.of(1, 2, 3); // Java 9+, immutable
// ints.add(4); → UnsupportedOperationException
ArrayList vs LinkedList
| ArrayList | LinkedList | |
|---|---|---|
| get(i) | O(1) | O(n) |
| add at end | O(1) amortised | O(1) |
| add in middle | O(n) | O(n) to find, O(1) to insert |
| Memory | Compact (single array) | ~2–3x overhead (nodes) |
In practice, ArrayList wins 95% of the time thanks to CPU cache locality.