← all cheatsheets

cheatsheet · while you code

Java API by collection

Use it when: You're coding and forgot a method name, or what it costs.

Pick the collection you're using. Each call says what it does and what it costs.

callwhat it doescost
a.clone() / Arrays.copyOf(a, n)Shallow copy of an array · Java MemoryO(n)
Arrays.equals(a, b) / Arrays.deepEquals(g, h)Compare arrays element by element (deep for 2D) · Java MemoryO(n)
new int[n] / new int[r][c]Zero-filled array / grid · Java Arrays & StringsO(n) / O(r·c)
arr.length · s.length() · list.size()Size of an array / a String / a collection · Java Arrays & StringsO(1)
Arrays.fill(a, v)Set every slot to v · Java Arrays & StringsO(n)
Arrays.copyOf(a, len) / copyOfRange(a, from, to)New array; pads with 0 or null; to is exclusive · Java Arrays & StringsO(len)
Arrays.toString(a) / Arrays.deepToString(g)Readable text for debugging · Java Arrays & StringsO(n)
Arrays.sort(a) / Arrays.sort(boxed, cmp)Sort primitives / sort objects with a Comparator · Java Arrays & StringsO(n log n)
Arrays.asList(T...)Fixed-size List view over an object array · Java Arrays & StringsO(1)
System.arraycopy(src, i, dst, j, len)Fast block copy between arrays · Java Arrays & StringsO(len)
Arrays.sort(a) / Arrays.sort(objs, cmp)Sort primitives / sort objects with a comparator · Java ToolkitO(n log n)
Arrays.toString(arr)Readable print of an array while debugging · MindsetO(n)
Arrays.sort(a)Sort an array · Big-OO(n log n)
Arrays.fill(memo, -1)Mark every memo slot as "not solved yet" · RecursionO(n)
Arrays.binarySearch(sorted, key)Index of key, or −(insertion point) − 1 when missing · Math & NumbersO(log n)
new int[n]Fixed-size array, filled with 0 · Arrays & StringsO(n)
Arrays.copyOf(a, len)Copy (and truncate or pad with 0) into a new array · Arrays & StringsO(len)
Arrays.sort(int[])In-place sort of primitives (dual-pivot quicksort) · Arrays & StringsO(n log n)
Arrays.equals(a, b) / Arrays.toString(a)Compare / print contents (a == b only compares references) · Arrays & StringsO(n)
new int[rows][cols]A rows × cols grid of 0s (one int[] per row) · MatricesO(rows·cols)
grid.length / grid[0].lengthNumber of rows / columns · MatricesO(1)
grid[r].clone()Copy one row · MatricesO(cols)
Arrays.stream(grid).map(int[]::clone).toArray(int[][]::new)Deep copy in one line · MatricesO(rows·cols)
Arrays.fill(grid[r], 0)Set a whole row to one value · MatricesO(cols)
Arrays.deepToString(grid) / Arrays.deepEquals(a, b)Print / compare 2D contents · MatricesO(rows·cols)
Arrays.sort(int[] a)Sorts primitives in place (dual-pivot quicksort) · Two PointersO(n log n)
Arrays.sort(T[] a, Comparator)Stable sort for objects (TimSort) · Two PointersO(n log n)
Arrays.copyOf(a, k)The first k elements as a new array (handy for checking in-place answers) · Two PointersO(k)
Arrays.equals(a, b)Compare two count arrays element by element · Sliding WindowO(len)
Arrays.binarySearch(int[] a, int key)Index of key, or -(insertionPoint) − 1 if absent · Binary SearchO(log n)
Arrays.binarySearch(a, from, to, key)Same, on the range [from, to) — to is exclusive · Binary SearchO(log(to − from))
Arrays.binarySearch(T[] a, key, cmp)Objects sorted by a comparator · Binary SearchO(log n) comparisons
Arrays.sort(int[] a, from, to)Sorts only a[from..to-1] · SortingO(m log m), m = to − from
Arrays.parallelSort(a)Multi-threaded sort for very large arrays · SortingO(n log n) work
int[] best = {Integer.MIN_VALUE}A mutable holder instead of a field · Tree RecursionO(1)
a == b on TreeNodeCompare nodes by reference (LCA's p and q) · Tree RecursionO(1)
new Node[26]Child slots for 'a'..'z'; all start as null · TriesO(26) to allocate
Arrays.fill(dist, -1)Mark every vertex "unvisited" · GraphsO(V)
new boolean[n] / new boolean[rows][cols]Visited flags, all false by default · GraphsO(V)
Arrays.fill(dist, Integer.MAX_VALUE)Mark every node unreached · Shortest PathsO(V)
dist.clone()Snapshot for round-limited Bellman–Ford · Shortest PathsO(V)
int[] parent = new int[n]; parent[i] = iMake n singleton sets · Union-Find & MSTO(n)
Arrays.sort(edges, Comparator.comparingInt(e -> e[2]))Sort edge rows {u, v, w} by weight (Kruskal) · Union-Find & MSTO(E log E)
Arrays.fill(best, Integer.MAX_VALUE)Initialise "cheapest link" array (dense Prim) · Union-Find & MSTO(n)
int[] indegree = new int[n]In-degree counters, all start at 0 · Topo SortO(n)
Arrays.fill(color, 0)Reset colours between test cases · Topo SortO(n)
Arrays.fill(tin, -1)Mark every vertex unvisited before a low-link DFS · Advanced GraphsO(n)
Arrays.sort(nums)Sort so duplicates are adjacent and pruning can break · BacktrackingO(n log n)
boolean[] used = new boolean[n]Which indices are already in the path (permutations) · BacktrackingO(1) per check
Arrays.fill(row, '.')Initialise a board row (N-Queens, Sudoku) · BacktrackingO(n)
Arrays.fill(a, 1)Give every child one candy to start · GreedyO(n)
Arrays.sort(int[][] a, Comparator)Stable sort of the rows (TimSort) · IntervalsO(n log n)
new int[n + 1]Table of n+1 zeros (index = state). Off-by-one +1 so dp[n] exists. · Dynamic ProgrammingO(n)
new int[m + 1][n + 1]2D table, row 0 / column 0 = empty prefix · Dynamic ProgrammingO(m·n)
Arrays.fill(dp, -1)Mark every state "not computed" (or fill with a sentinel like amount + 1) · Dynamic ProgrammingO(n)
new Integer[m][n] / new Boolean[n]Memo where null = unknown (when 0 / false are real answers) · Dynamic ProgrammingO(m·n)
Arrays.binarySearch(a, 0, size, x)Index of x, or −(insertionPoint) − 1 if absent — usable for LIS patience · Dynamic ProgrammingO(log n)
Arrays.stream(nums).sum()Total of an int[] (Partition Equal Subset Sum) · Dynamic ProgrammingO(n)
Arrays.fill(buy, Integer.MIN_VALUE / 2)Start every "holding share t" state as impossible · DP PatternsO(k)
new boolean[m + 1][n + 1]Two-string table; row 0 / column 0 = the empty prefix · DP PatternsO(m·n)
Arrays.sort(jobs, (a, b) -> Integer.compare(a[1], b[1]))Sort int[][] rows by their end time · DP PatternsO(n log n)
new Integer[n][n] / new Boolean[m + 1][n + 1]Memo where null = unknown (leads can be 0 or negative; matches can be false) · DP PatternsO(n²) / O(m·n)
Arrays.parallelPrefix(long[] a, Long::sum)In-place inclusive running sums · Range Queries & StringsO(n)
Arrays.stream(a).distinct().sorted().toArray()Sorted distinct values for coordinate compression · Range Queries & StringsO(n log n)
Arrays.binarySearch(sorted, x)Rank of x after compression (index, ≥ 0 if present) · Range Queries & StringsO(log n)
a.regionMatches(i, b, j, len)Compare two slices without substring() · Range Queries & StringsO(len)