// Basic Mathematical OperationsMath.max(a, b) // Maximum of two numbersMath.min(a, b) // Minimum of two numbersMath.abs(n) // Absolute valueMath.pow(a, b) // Power operationMath.sqrt(n) // Square rootMath.cbrt(n) // Cube root// Rounding OperationsMath.ceil(n) // Round up to nearest integerMath.floor(n) // Round down to nearest integerMath.round(n) // Round to nearest integer// Logarithmic OperationsMath.log(n) // Natural logarithmMath.log10(n) // Base-10 logarithmMath.exp(n) // e raised to power n// Other Utility MethodsMath.random() // Random number between 0.0 and 1.0Math.hypot(x, y) // Sqrt(x^2 + y^2)Math.toDegrees(rad) // Radians to degreesMath.toRadians(deg) // Degrees to radians
// Prime number checkboolean isPrime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true;}// GCD (Greatest Common Divisor)int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b);}// LCM (Least Common Multiple)int lcm(int a, int b) { return (a * b) / gcd(a, b);}// Fast Power (Binary Exponentiation)long power(long base, long exp, long mod) { long result = 1; base %= mod; while (exp > 0) { if ((exp & 1) == 1) result = (result * base) % mod; base = (base * base) % mod; exp >>= 1; } return result;}
// Adding Elementslist.add(element) // Add elementlist.add(index, element) // Add at indexlist.addAll(collection) // Add all elements// Accessing and Modifyinglist.get(index) // Get elementlist.set(index, element) // Set elementlist.remove(index) // Remove by indexlist.remove(Object) // Remove objectlist.clear() // Remove all// Information Methodslist.size() // Get sizelist.isEmpty() // Check if emptylist.contains(object) // Check existencelist.indexOf(object) // First occurrencelist.lastIndexOf(object) // Last occurrence// Conversion Methodslist.toArray() // Convert to arraylist.toArray(new Type[0]) // Convert to typed arraylist.subList(from, to) // Get sublist view
// Construction and AppendingStringBuilder sb = new StringBuilder();sb.append(str) // Add stringsb.append(char) // Add charactersb.insert(offset, str) // Insert at position// Modification Methodssb.delete(start, end) // Delete rangesb.deleteCharAt(index) // Delete charactersb.reverse() // Reverse stringsb.replace(start, end, str) // Replace range// Access Methodssb.substring(start, end) // Get substringsb.charAt(index) // Get charactersb.length() // Get lengthsb.setLength(newLength) // Set lengthsb.indexOf(str) // Find string// Conversionsb.toString() // Convert to Stringsb.setCharAt(index, char) // Set character
Strings
String Operations
// Basic Operationsstr.length() // Lengthstr.charAt(index) // Get characterstr.substring(start) // Substring from startstr.substring(start, end) // Substring range// Searching Methodsstr.indexOf(str) // Find first occurrencestr.lastIndexOf(str) // Find last occurrencestr.contains(str) // Check if containsstr.startsWith(prefix) // Check prefixstr.endsWith(suffix) // Check suffix// Case Conversionstr.toLowerCase() // Convert to lowercasestr.toUpperCase() // Convert to uppercasestr.trim() // Remove whitespace// Modification Methodsstr.replace(old, new) // Replace all occurrencesstr.replaceAll(regex, repl) // Replace with regexstr.split(regex) // Split to arraystr.join(delimiter, elements) // Join elements// Pattern Matchingstr.matches(regex) // Match regex// Conversion Methodsstr.toCharArray() // Convert to char array// Comparison Methodsstr.compareTo(str2) // Compare stringsstr.equals(str2) // Check equalitystr.equalsIgnoreCase(str2) // Case-insensitive equals
Wrapper Classes
Integer Operations
// Conversion MethodsInteger.parseInt(str) // String to intInteger.toString(num) // Int to StringInteger.valueOf(str) // String to Integer object// Bit OperationsInteger.bitCount(n) // Count set bitsInteger.highestOneBit(n) // Highest set bitInteger.lowestOneBit(n) // Lowest set bitInteger.numberOfLeadingZeros(n)Integer.numberOfTrailingZeros(n)// ConstantsInteger.MAX_VALUE // Maximum valueInteger.MIN_VALUE // Minimum value
Character Operations
// Character CheckingCharacter.isDigit(ch) // Check if digitCharacter.isLetter(ch) // Check if letterCharacter.isLetterOrDigit(ch) // Check alphanumericCharacter.isWhitespace(ch) // Check whitespaceCharacter.isUpperCase(ch) // Check uppercaseCharacter.isLowerCase(ch) // Check lowercase// Case ConversionCharacter.toUpperCase(ch) // Convert to uppercaseCharacter.toLowerCase(ch) // Convert to lowercase
LinkedList
LinkedList Operations
// Adding Elementslist.add(element) // Add at endlist.addFirst(element) // Add at startlist.addLast(element) // Add at end// Removing Elementslist.remove(element) // Remove elementlist.removeFirst() // Remove from startlist.removeLast() // Remove from end// Accessing Elementslist.getFirst() // Get first elementlist.getLast() // Get last elementlist.peek() // View first elementlist.peekFirst() // View first elementlist.peekLast() // View last element// Queue-like Operationslist.poll() // Remove and return firstlist.pollFirst() // Remove and return firstlist.pollLast() // Remove and return lastlist.offer(element) // Add at endlist.offerFirst(element) // Add at startlist.offerLast(element) // Add at end
Stack
Stack Operations
// Basic Operationsstack.push(element) // Add elementstack.pop() // Remove and return topstack.peek() // View top elementstack.empty() // Check if emptystack.search(element) // Find positionstack.size() // Get sizestack.clear() // Remove all elements
Queue/Deque
Queue Operations
// Basic Queue Operationsqueue.offer(element) // Add elementqueue.poll() // Remove and return frontqueue.peek() // View front elementqueue.size() // Get sizequeue.isEmpty() // Check if empty
Deque Operations
// Double-ended Queue Operationsdeque.addFirst(element) // Add at frontdeque.addLast(element) // Add at enddeque.removeFirst() // Remove from frontdeque.removeLast() // Remove from enddeque.getFirst() // Get first elementdeque.getLast() // Get last elementdeque.peekFirst() // View first elementdeque.peekLast() // View last element
Recursion Patterns
1. Basic Recursion
void basic(int n) { if (n == 0) return; // Base case // Process current basic(n - 1); // Recursive call}
2. Multiple Recursion Calls
int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n-1) + fibonacci(n-2);}
3. Recursion with Helper Function
int solve(int n) { return helper(n, additional_params);}int helper(int n, /* additional parameters */) { if (base_case) return value; // Process and make recursive calls return helper(modified_params);}
4. Backtracking Pattern
void backtrack(int[] arr, int index) { if (index == arr.length) { // Process solution return; } for (int i = index; i < arr.length; i++) { // Make change swap(arr, index, i); // Recurse backtrack(arr, index + 1); // Undo change (backtrack) swap(arr, index, i); }}
5. Divide and Conquer
T divideConquer(problem) { if (problem is small enough) { solve directly } // Divide subproblems = divide(problem) // Conquer subresults = [] for (subproblem : subproblems) { result = divideConquer(subproblem) subresults.add(result) } // Combine return combine(subresults)}