Lists: ArrayList & LinkedList
A list is an ordered collection that can grow and shrink, allows duplicates, and gives access by position. It's the collection you'll use most. List is the interface; ArrayList is the usual implementation, with LinkedList and immutable lists for special cases.
Creating and using an ArrayList
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> tasks = new ArrayList<>(); tasks.add("Write report"); tasks.add("Email Ana"); tasks.add(0, "Coffee"); System.out.println(tasks); System.out.println(tasks.size() + " tasks, first: " + tasks.get(0)); tasks.set(2, "Email Ana and Ben"); tasks.remove("Coffee"); System.out.println(tasks); System.out.println(tasks.contains("Write report") + " " + tasks.indexOf("Email Ana and Ben")); } }
[Coffee, Write report, Email Ana] 3 tasks, first: Coffee [Write report, Email Ana and Ben] true 1
| Method | Does |
|---|---|
add(e), add(i, e) | Append, or insert at index |
get(i), set(i, e) | Read or replace by index |
remove(i), remove(obj) | Remove by index or by value |
size(), isEmpty() | Count |
contains(obj), indexOf(obj) | Search (uses equals) |
clear(), addAll(c), removeIf(p) | Bulk operations |
Iterating
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> prices = new ArrayList<>(List.of(12, 45, 8, 30)); for (int p : prices) System.out.print(p + " "); System.out.println(); for (int i = 0; i < prices.size(); i++) prices.set(i, prices.get(i) * 2); prices.forEach(p -> System.out.print(p + " ")); System.out.println(); prices.removeIf(p -> p > 50); System.out.println(prices); } }
12 45 8 30 24 90 16 60 [24, 16]
Use for-each to read, an index loop to modify by position, and removeIf to remove matching elements. Removing elements inside a for-each loop throws ConcurrentModificationException.
Immutable lists
import java.util.List; public class Main { public static void main(String[] args) { List<String> sizes = List.of("S", "M", "L"); System.out.println(sizes); try { sizes.add("XL"); } catch (UnsupportedOperationException e) { System.out.println("List.of creates an unmodifiable list"); } } }
[S, M, L] List.of creates an unmodifiable list
List.of(…) creates a fixed list (no null elements allowed); List.copyOf(list) makes an unmodifiable copy. Unmodifiable lists are great for constants and return values. Wrap one in new ArrayList<>(…) when you need to change it.
The remove(int) trap
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> nums = new ArrayList<>(List.of(10, 20, 30)); nums.remove(1); System.out.println(nums); nums.remove(Integer.valueOf(30)); System.out.println(nums); } }
[10, 30] [10]
With a list of integers, remove(1) removes the element at index 1. To remove the value, pass an Integer object.
ArrayList vs LinkedList
| ArrayList | LinkedList | |
|---|---|---|
| Storage | Resizable array | Chain of nodes |
get(i) | Fast | Slow (walks the chain) |
| Add/remove at end | Fast | Fast |
| Add/remove at start | Slow (shifts elements) | Fast |
| Memory | Compact | Extra per element |
In practice ArrayList is faster for almost everything, thanks to memory locality. For queues and stacks use ArrayDeque (lesson 44). Because you declare variables as List, switching implementation is a one-word change.
Useful helpers
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { List<String> names = new ArrayList<>(List.of("Mei", "Ana", "Lucas", "Ben")); Collections.sort(names); System.out.println(names); Collections.reverse(names); System.out.println(names); System.out.println(Collections.max(names) + " " + Collections.frequency(names, "Ana")); System.out.println(names.subList(1, 3)); System.out.println(names.reversed()); } }
[Ana, Ben, Lucas, Mei] [Mei, Lucas, Ben, Ana] Mei 1 [Lucas, Ben] [Ana, Ben, Lucas, Mei]
reversed() (Java 21's "sequenced collections") returns a reversed view; lists also gained getFirst(), getLast(), addFirst and removeLast.
Try it yourself
Keep a shopping list: add items, avoid duplicates, remove bought items and print what's left.
Still to buy: [milk, eggs]