02.02 · Cheat sheet
Java for Coding Tests
Everything in Java you reach for during a timed test, and the traps that cost marks.
Which collection?
| Need | Use |
|---|---|
| Growable list | ArrayList |
| Stack or queue | ArrayDeque |
| Key → value | HashMap |
| Unique items | HashSet |
| Sorted keys, floor / ceiling | TreeMap, TreeSet |
| Smallest first | PriorityQueue |
| Largest first | new PriorityQueue<>(Collections.reverseOrder()) |
Snippets
Map<String, Integer> cnt = new HashMap<>();
cnt.merge(w, 1, Integer::sum); // count
int c = cnt.getOrDefault(w, 0);
Deque<Integer> st = new ArrayDeque<>(); // push, pop, peek
Deque<Integer> q = new ArrayDeque<>(); // offer, poll, peek
PriorityQueue<int[]> pq =
new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));
TreeMap<Integer, Integer> tm = new TreeMap<>();
Integer f = tm.floorKey(x); // largest key <= x, or null
solulu Pro
Continue this cheat sheet with Pro
Inside: 8 sections.
- Which collection?
- Snippets
- Sorting
- Strings
- Integer traps
- Arrays and helpers
- +2 more
Lifetime ₹1,999 ₹999 · Launch offer
Item 6 of 12 · updated 2026-10-13