Java Collections: List, Map, Queue & Deque
April 28, 2026
π¦ Java Collections: List, Map, Queue & Deque
Java's collections framework gives you a toolbox of data structures. The trick is knowing which tool fits the job.
1οΈβ£ ArrayList vs LinkedList
Both implement List β same API, very different internals.
List<Person> people = new ArrayList<>(); // backed by a dynamic array
List<String> names = new LinkedList<>(seed); // backed by doubly-linked nodesArrayList | LinkedList | |
|---|---|---|
| Get by index | O(1) β direct jump | O(n) β walks the chain |
| Add to end | O(1) amortised | O(1) |
| Insert/remove middle | O(n) β shifts elements | O(1) once at the node |
| Memory | Compact (contiguous) | Higher overhead (prev/next pointers per node) |
Default to ArrayList. Reach for LinkedList only when you're inserting or removing heavily at the front or middle.
2οΈβ£ List.of(...) β Immutable List
List<String> names = List.of("Alice", "Bob", "Charlie");
names.add("David"); // π₯ UnsupportedOperationException- Fixed size, no nulls, order preserved.
- Great for constants. If you need to mutate it, wrap it:
new ArrayList<>(names)ornew LinkedList<>(names).
3οΈβ£ HashMap
Map<String, Object> map = new HashMap<>();
map.put("john", new Person("John", 30));
map.get("john"); // O(1) average- Keyβvalue lookup/insert/delete all O(1) average.
- No guaranteed key order β use
LinkedHashMap(insertion order) orTreeMap(sorted) if you need it. - Allows one
nullkey and multiplenullvalues.
Use HashMap any time you need fast lookup by a key rather than by position.
4οΈβ£ Queue β First In, First Out (FIFO)
Think of a checkout line β first person in, first served.
Queue<String> queue = new LinkedList<>();
queue.offer("Alice"); // add to tail
queue.offer("Bob");
queue.offer("Charlie");
queue.poll(); // removes & returns "Alice"
queue.peek(); // returns "Bob" without removing| Action | Safe (returns null/false) | Throws on failure |
|---|---|---|
| Add | offer() | add() |
| Remove head | poll() | remove() |
| Inspect head | peek() | element() |
Use when: processing tasks in arrival order β job queues, BFS graph traversal, event pipelines.
5οΈβ£ Deque β Double-Ended Queue
Pronounced "deck". Works as both a queue and a stack β you can add and remove from either end.
Deque<String> deque = new ArrayDeque<>();
// As a queue (FIFO)
deque.offerLast("Alice");
deque.offerLast("Bob");
deque.pollFirst(); // "Alice"
// As a stack (LIFO)
deque.push("Alice"); // pushes to head
deque.push("Bob");
deque.pop(); // "Bob" β last in, first out| Front (head) | Back (tail) | |
|---|---|---|
| Add | offerFirst() / push() | offerLast() / offer() |
| Remove | pollFirst() / pop() | pollLast() |
| Inspect | peekFirst() | peekLast() |
ArrayDeque vs LinkedList as Deque: prefer ArrayDeque β no node allocation overhead, faster in practice. Use LinkedList only if you need null elements or the List interface on the same object.
Quick Mental Model
ArrayList β indexed reads, add to end β default List choice
LinkedList β front inserts/removes, or as Deque
List.of() β immutable constants
HashMap β fast keyβvalue lookup, no order
Queue β FIFO line β use LinkedList or ArrayDeque
Deque β both ends open β use ArrayDequePeace... π