Java Cheatsheet
58 essential methods for coding interviews
Showing 58 of 58 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
Arrays.sort | Arrays.sort(array) | Arrays.sort(array, comparator) | Sorts array in-place using dual-pivot Quicksort (primitives) or TimSort (objects) | O(n log n) | essential |
Arrays.binarySearch | Arrays.binarySearch(array, key) | Searches sorted array for key, returns index or (-(insertion point) - 1) | O(log n) | essential |
Arrays.asList | Arrays.asList(array) | Returns fixed-size list backed by array | O(1) | essential |
ArrayList.add | list.add(element) | list.add(index, element) | Appends element or inserts at index | O(1) amortized, O(n) at index | essential |
ArrayList.get | list.get(index) | Returns element at specified index | O(1) | essential |
ArrayList.remove | list.remove(index) | list.remove(object) | Removes element by index or first occurrence of object | O(n) | essential |
ArrayList.contains | list.contains(object) | Returns true if list contains specified element | O(n) | essential |
Collections.reverse | Collections.reverse(list) | Reverses list in-place | O(n) | essential |
Collections.sort | Collections.sort(list) | Collections.sort(list, comparator) | Sorts list in-place using TimSort | O(n log n) | essential |
HashMap.put | map.put(key, value) | Associates key with value, returns previous value or null | O(1) average | essential |
HashMap.get | map.get(key) | Returns value for key, or null if not present | O(1) average | essential |
HashMap.getOrDefault | map.getOrDefault(key, defaultValue) | Returns value for key, or default if not present | O(1) average | essential |
HashMap.containsKey | map.containsKey(key) | Returns true if map contains the key | O(1) average | essential |
HashMap.computeIfAbsent | map.computeIfAbsent(key, mappingFunction) | Computes value using function if key is absent, returns value | O(1) average | essential |
HashMap.keySet | map.keySet() | Returns Set view of all keys | O(1) | essential |
HashMap.entrySet | map.entrySet() | Returns Set of key-value entry pairs for iteration | O(1) | essential |
HashSet.add | set.add(element) | Adds element to set, returns true if not already present | O(1) average | essential |
HashSet.contains | set.contains(element) | Returns true if set contains element | O(1) average | essential |
HashSet.remove | set.remove(element) | Removes element from set, returns true if present | O(1) average | essential |
String.charAt | str.charAt(index) | Returns character at specified index | O(1) | essential |
String.substring | str.substring(beginIndex) | str.substring(beginIndex, endIndex) | Returns substring (endIndex is exclusive) | O(n) | essential |
String.toCharArray | str.toCharArray() | Converts string to character array | O(n) | essential |
String.split | str.split(regex) | str.split(regex, limit) | Splits string by regex pattern into array | O(n) | essential |
String.indexOf | str.indexOf(char) | str.indexOf(substring) | Returns first index of char/substring, or -1 if not found | O(n*m) for substring | essential |
String.equals | str1.equals(str2) | Compares string content for equality | O(n) | essential |
StringBuilder | new StringBuilder() | sb.append(x) | sb.toString() | Mutable string builder for efficient concatenation | O(1) amortized for append | essential |
StringBuilder.reverse | sb.reverse() | Reverses the string builder in-place | O(n) | essential |
Character.isDigit/isLetter/isLetterOrDigit | Character.isDigit(c) | Character.isLetter(c) | Checks if character is digit, letter, or alphanumeric | O(1) | essential |
Integer.parseInt | Integer.parseInt(str) | Integer.valueOf(str) | Parses string to int primitive or Integer object | O(n) | essential |
Math.max/min | Math.max(a, b) | Math.min(a, b) | Returns larger or smaller of two values | O(1) | essential |
Math.abs | Math.abs(value) | Returns absolute value | O(1) | essential |
Stream.filter | stream.filter(predicate) | Returns stream with elements matching predicate | O(n) | essential |
Stream.map | stream.map(function) | Transforms each element using function | O(n) | essential |
Stream.reduce | stream.reduce(identity, accumulator) | Reduces stream to single value using accumulator function | O(n) | essential |
Stream.collect | stream.collect(Collectors.toList()) | Collects stream elements into a collection | O(n) | essential |
Arrays.fill | Arrays.fill(array, value) | Arrays.fill(array, from, to, value) | Fills array or range with specified value | O(n) | common |
Arrays.copyOf | Arrays.copyOf(array, newLength) | Creates new array with specified length, copying elements | O(n) | common |
ArrayList.set | list.set(index, element) | Replaces element at index, returns old element | O(1) | common |
ArrayList.subList | list.subList(fromIndex, toIndex) | Returns view of portion of list (fromIndex inclusive, toIndex exclusive) | O(1) | common |
Collections.max/min | Collections.max(collection) | Collections.min(collection) | Returns maximum or minimum element | O(n) | common |
HashMap.merge | map.merge(key, value, remappingFunction) | Merges value with existing using function, or puts if absent | O(1) average | common |
HashSet.retainAll | set.retainAll(collection) | Retains only elements in both (intersection) | O(n) | common |
HashSet.addAll | set.addAll(collection) | Adds all elements from collection (union) | O(n) | common |
String.compareTo | str1.compareTo(str2) | Compares strings lexicographically, returns negative/zero/positive | O(n) | common |
String.startsWith/endsWith | str.startsWith(prefix) | str.endsWith(suffix) | Checks if string starts/ends with specified substring | O(m) where m is prefix/suffix length | common |
String.toLowerCase/toUpperCase | str.toLowerCase() | str.toUpperCase() | Returns new string with all characters converted to lower/upper case | O(n) | common |
String.trim | str.trim() | Returns string with leading and trailing whitespace removed | O(n) | common |
String.replace | str.replace(oldChar, newChar) | str.replace(target, replacement) | Returns new string with all occurrences replaced | O(n) | common |
String.valueOf | String.valueOf(value) | Converts any primitive or object to string | O(n) | common |
Math.pow | Math.pow(base, exponent) | Returns base raised to exponent power (returns double) | O(1) | common |
Math.sqrt | Math.sqrt(value) | Returns square root | O(1) | common |
Math.floor/ceil/round | Math.floor(x) | Math.ceil(x) | Math.round(x) | Rounds down, up, or to nearest integer | O(1) | common |
Collectors.groupingBy | stream.collect(Collectors.groupingBy(classifier)) | Groups elements by classifier function into Map | O(n) | common |
Stream.sorted | stream.sorted() | stream.sorted(comparator) | Returns stream with elements sorted | O(n log n) | common |
Stream.distinct | stream.distinct() | Returns stream with duplicate elements removed | O(n) | common |
Stream.anyMatch/allMatch/noneMatch | stream.anyMatch(predicate) | Tests if any/all/none elements match predicate | O(n) | common |
Stream.findFirst/findAny | stream.findFirst() | Returns Optional of first element (or any for parallel) | O(n) | common |
IntStream.range | IntStream.range(start, end) | IntStream.rangeClosed(start, end) | Creates stream of integers from start to end (exclusive/inclusive) | O(n) | common |