Java Collections Cheat Sheet: Which to Use & How
A Java collections cheat sheet: a decision guide for which collection to use, plus everyday methods for List, Set, Map, Queue, Stack, and PriorityQueue in one place.
The Java Collections Framework is huge, and remembering which class to use — and its exact methods — is genuinely hard. This collections cheat sheet puts it all in one place: which collection to pick, when, and the methods you use daily. Bookmark it for interviews and everyday coding alike.
Which collection should I use? (the decision guide)
| I need… | Use |
|---|---|
| An ordered list, allows duplicates | ArrayList |
| Unique items, fast lookup | HashSet |
| Unique items, kept sorted | TreeSet |
| Key → value pairs, fast lookup | HashMap |
| Key → value, kept sorted by key | TreeMap |
| FIFO queue | LinkedList / ArrayDeque |
| LIFO stack | ArrayDeque |
| Smallest/largest always ready | PriorityQueue |
When unsure: ArrayList for lists, HashMap for key-value, HashSet for uniqueness. Those three cover most needs.
List (ArrayList)
List<String> list = new ArrayList<>(); list.add("a"); // add list.get(0); // read by index → "a" list.set(0, "b"); // update list.remove(0); // remove by index list.size(); // count list.contains("b"); // membership list.removeIf(s -> s.isEmpty()); // safe conditional remove
Set (HashSet)
Set<String> set = new HashSet<>(); set.add("x"); // add (duplicates ignored) set.contains("x"); // O(1) membership → true set.remove("x"); // remove set.size(); // count of unique items
Map (HashMap)
Map<String, Integer> map = new HashMap<>(); map.put("a", 1); // add/update pair map.get("a"); // read → 1 (null if absent) map.getOrDefault("z", 0); // safe read with default → 0 map.containsKey("a"); // true map.remove("a"); // delete map.merge("a", 1, Integer::sum); // counting trick: +1 // iterate for (var e : map.entrySet()) { e.getKey(); e.getValue(); }
Queue & Stack (ArrayDeque)
// Queue (FIFO) Queue<Integer> q = new LinkedList<>(); q.offer(1); q.poll(); q.peek(); // add back, remove front, look // Stack (LIFO) — prefer ArrayDeque over the old Stack class Deque<Integer> stack = new ArrayDeque<>(); stack.push(1); stack.pop(); stack.peek(); // add/remove/look top // Min-heap (smallest first) PriorityQueue<Integer> heap = new PriorityQueue<>(); heap.offer(5); heap.poll(); // poll returns the smallest
Handy utilities
List.of(1, 2, 3); // immutable list Map.of("a", 1, "b", 2); // immutable map Collections.sort(list); // sort in place list.sort(Comparator.reverseOrder()); // custom sort new ArrayList<>(otherCollection); // copy
HashMap keys or in a HashSet, override equals() and hashCode(). And never modify a collection inside a for-each loop — use removeIf instead.
Even after years of Java, I still glance at a reference like this — the collections API is broad, and there's zero shame in checking "was it offer or add for a queue?" What sticks with practice is the decision: given a problem, which structure fits? That judgment — list vs set vs map, and their sorted/ordered variants — is what interviewers test and what makes your code efficient. Bookmark this for the syntax, and read the full Collections lesson to build the deeper intuition. With this and the core Java cheat sheet, you've got Java's essentials at your fingertips. Happy coding!