ArrayList
ArrayList is a resizable array implementation. It's the most commonly used List — fast for random access but slower for insertions/deletions in the middle.
Example
import java.util.ArrayList;
import java.util.Collections;
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add(1, "Blueberry"); // insert at index 1
// Access and modify
fruits.get(0); // "Apple"
fruits.set(0, "Avocado"); // replace
fruits.remove("Banana"); // remove by value
fruits.remove(0); // remove by index
// Sorting
Collections.sort(fruits);
// Iteration
for (String fruit : fruits) {
System.out.println(fruit);
}
// Stream operations (Java 8+)
fruits.stream()
.filter(f -> f.startsWith("B"))
.forEach(System.out::println); LinkedList
LinkedList uses a doubly-linked list structure. It's faster for insertions and deletions but slower for random access.
Example
import java.util.LinkedList;
LinkedList<String> tasks = new LinkedList<>();
tasks.add("Task 1");
tasks.add("Task 2");
tasks.addFirst("Urgent Task"); // add to front
tasks.addLast("Low Priority"); // add to end
tasks.getFirst(); // "Urgent Task"
tasks.getLast(); // "Low Priority"
tasks.removeFirst();
tasks.removeLast();
// LinkedList as Queue
tasks.offer("New Task"); // add to end
tasks.poll(); // remove from front
tasks.peek(); // view front without removing 