Java Methods Cheat Sheet For DSA Problems
Java Methods Cheat Sheet For DSA Problems
Problems
String Methods
Basic Operations
va
● length() - Returns the length of the string
● charAt(int index) - Returns the character at the specified index
de
● substring(int beginIndex) - Returns substring from beginIndex to end
● substring(int beginIndex, int endIndex) - Returns substring from
ch
beginIndex to endIndex-1
● isEmpty() - Checks if string length is 0
Sa
● toCharArray() - Converts string to character array
String Comparison
et
● equals(Object obj) - Case-sensitive comparison with another string
ne
String Modification
Searching
va
● concat(String str) - Concatenates another string
de
StringBuilder Methods
ch
Basic Operations
Sa
● StringBuilder() - Constructor creates empty builder with capacity 16
● StringBuilder(String str) - Constructor with initial string
● length() - Returns length (character count)
et
● capacity() - Returns current capacity
● charAt(int index) - Returns char at specified index
ne
Modification
:J
va
range
● Arrays.sort(array, Comparator<? super T> c) - Sorts with custom
de
comparator
● Arrays.binarySearch(array, key) - Binary search on sorted array
● Arrays.binarySearch(array, fromIndex, toIndex, key) - In specific
ch
range
● Arrays.fill(array, val) - Fills entire array with specified value
Sa
● Arrays.fill(array, fromIndex, toIndex, val) - Fills range with value
Array Conversion
et
● Arrays.asList(T... a) - Converts array to fixed-size List
ne
Java 8+ Enhancements
:J
Basic Operations
Searching
Bulk Operations
va
● addAll(Collection<? extends E> c) - Appends all elements
● addAll(int index, Collection<? extends E> c) - Inserts all at index
de
● removeAll(Collection<?> c) - Removes all elements in c
● retainAll(Collection<?> c) - Retains only elements in c
ch
● containsAll(Collection<?> c) - True if contains all elements
Sa
List Views
predicate
● sort(Comparator<? super E> c) - Sorts list with provided comparator
In
Basic Operations
nk
Bulk Operations
● addAll(Collection<? extends E> c) - Adds all elements
● removeAll(Collection<?> c) - Removes all elements in c
● retainAll(Collection<?> c) - Retains only elements in c
● containsAll(Collection<?> c) - True if contains all elements
Iteration
Java 8+ Enhancements
va
● stream() - Returns a sequential Stream
● forEach(Consumer<? super E> action) - Performs action for each element
de
● removeIf(Predicate<? super E> filter) - Removes all elements matching
predicate
ch
HashMap (and Map) Methods
Basic Operations Sa
et
● HashMap<K, V>() - Constructor creates empty map
ne
Bulk Operations
Map Views
Java 8+ Enhancements
va
remappingFunction) - Computes value
● computeIfAbsent(K key, Function<? super K, ? extends V>
de
mappingFunction) - If key absent
● computeIfPresent(K key, BiFunction<? super K, ? super V, ?
ch
extends V> remappingFunction) - If key present
● merge(K key, V value, BiFunction<? super V, ? super V, ?
Sa
extends V> remappingFunction) - Merges values
● remove() / poll() - Removes and returns head (poll returns null if empty)
● element() / peek() - Retrieves head without removing (peek returns null if empty)
:J
PriorityQueue Specific
ed
ordering
Li
Stack Methods
Basic Operations
va
de
ch
Sa
et
ne
ap
:J
In
ed
nk
Li