// Collections you'll use dailyMap<Integer,Integer> freq = new HashMap<>();freq.merge(x, 1, Integer::sum);freq.getOrDefault(x, 0);Deque<Integer> stack = new ArrayDeque<>(); // push/pop/peek. Don't use StackDeque<Integer> queue = new ArrayDeque<>(); // offer/poll/peekPriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> Integer.compare(a[0], b[0]));PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());TreeMap<Integer,Integer> tm = new TreeMap<>(); // floorKey, ceilingKey, firstKey, pollFirstEntryTreeSet<Integer> ts = new TreeSet<>(); // floor, ceiling, higher, lowerList<List<Integer>> graph = new ArrayList<>();for (int i = 0; i < n; i++) graph.add(new ArrayList<>());int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); // avoid a[0]-b[0] overflowArrays.fill(dp, -1);long mid = lo + (hi - lo) / 2; // avoid overflow
Templates
// Binary search on answer: first value where ok(x) is trueint lo = 0, hi = MAX;while (lo < hi) { int mid = lo + (hi - lo) / 2; if (ok(mid)) hi = mid; else lo = mid + 1; }// Union-Find with path compression + union by rankint find(int x) { return parent[x] == x ? x : (parent[x] = find(parent[x])); }// DijkstraPriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> Integer.compare(a[1], b[1]));// skip stale entries: if (d > dist[u]) continue;
Gotchas
Integer compared with == beyond the -128..127 cache → use .equals
int overflow → use long for sums/products
Recursion depth (~10k frames) → use iterative BFS/DFS for large inputs
String concatenation in loops → StringBuilder
Fast I/O for competitive programming: BufferedReader + StringTokenizer