0% found this document useful (0 votes)
24 views7 pages

Java Methods Cheat Sheet For DSA Problems

This document is a comprehensive cheat sheet for Java methods used in Data Structures and Algorithms (DSA), covering various classes such as String, StringBuilder, Arrays, ArrayList, HashSet, HashMap, Queue, and Stack. It provides a detailed list of methods for basic operations, searching, sorting, and modifications along with their descriptions. Additionally, it includes enhancements introduced in Java 8 and beyond.

Uploaded by

ajayvidhey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Java Methods Cheat Sheet For DSA Problems

This document is a comprehensive cheat sheet for Java methods used in Data Structures and Algorithms (DSA), covering various classes such as String, StringBuilder, Arrays, ArrayList, HashSet, HashMap, Queue, and Stack. It provides a detailed list of methods for basic operations, searching, sorting, and modifications along with their descriptions. Additionally, it includes enhancements introduced in Java 8 and beyond.

Uploaded by

ajayvidhey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Methods Cheat Sheet for DSA

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

●​ equalsIgnoreCase(String str) - Case-insensitive comparison


●​ compareTo(String str) - Lexicographical comparison (returns int)
ap

●​ startsWith(String prefix) - Checks if string starts with prefix


●​ endsWith(String suffix) - Checks if string ends with suffix
:J

●​ contains(CharSequence seq) - Checks if string contains sequence


In

String Modification

●​ toLowerCase() - Converts to lowercase


ed

●​ toUpperCase() - Converts to uppercase


●​ trim() - Removes whitespace from both ends
nk

●​ replace(char oldChar, char newChar) - Replaces all occurrences of a


character
Li

●​ replace(CharSequence target, CharSequence replacement) - Replaces


sequence
●​ replaceAll(String regex, String replacement) - Replaces by regex
pattern

Searching

●​ indexOf(String str) - First occurrence of specified string


●​ indexOf(String str, int fromIndex) - First occurrence from index
●​ lastIndexOf(String str) - Last occurrence of specified string
●​ lastIndexOf(String str, int fromIndex) - Last occurrence before index
●​ matches(String regex) - Tells if string matches regex pattern

Splitting and Joining

●​ split(String regex) - Splits string based on regex, returns array


●​ split(String regex, int limit) - Splits with limit on array size
●​ join(CharSequence delimiter, CharSequence... elements) - Joins
elements with delimiter (static method)

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

●​ substring(int start) - Returns substring from start to end


●​ substring(int start, int end) - Returns substring from start to end-1
ap

Modification
:J

●​ append(X x) - Appends string representation of X (many overloads)


●​ insert(int offset, X x) - Inserts string representation of X at position
In

●​ delete(int start, int end) - Removes chars from start to end-1


●​ deleteCharAt(int index) - Removes char at specified position
ed

●​ replace(int start, int end, String str) - Replaces substring


●​ setCharAt(int index, char ch) - Sets char at specified position
nk

●​ reverse() - Reverses the sequence


●​ setLength(int newLength) - Sets the length (truncates or adds null chars)
Li

●​ toString() - Converts to String

Arrays Methods (java.util.Arrays)


Basic Operations

●​ Arrays.toString(array) - Returns string representation of array


●​ Arrays.deepToString(Object[][] array) - For multi-dimensional arrays
●​ Arrays.equals(array1, array2) - Compares arrays for equality
●​ Arrays.deepEquals(Object[][] a1, Object[][] a2) - For
multi-dimensional arrays
●​ Arrays.hashCode(array) - Returns hash code based on contents
●​ Arrays.deepHashCode(Object[][] array) - For multi-dimensional arrays

Searching and Sorting

●​ Arrays.sort(array) - Sorts array in ascending order


●​ Arrays.sort(array, int fromIndex, int toIndex) - Sorts specified

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

●​ Arrays.copyOf(original, newLength) - Copies and possibly resizes


●​ Arrays.copyOfRange(original, from, to) - Copies specified range
ap

Java 8+ Enhancements
:J

●​ Arrays.stream(array) - Returns a sequential stream


In

●​ Arrays.parallelSort(array) - Sorts using multiple threads


ed

ArrayList (and List) Methods


nk

Basic Operations

●​ ArrayList<E>() - Constructor creates empty list


Li

●​ ArrayList<E>(Collection<? extends E> c) - Constructor with collection


●​ size() - Returns number of elements
●​ isEmpty() - Checks if list contains no elements
●​ get(int index) - Returns element at position
●​ set(int index, E element) - Replaces element at position
●​ add(E e) - Appends element to end
●​ add(int index, E element) - Inserts element at position
●​ remove(int index) - Removes element at position
●​ remove(Object o) - Removes first occurrence of element
●​ clear() - Removes all elements

Searching

●​ contains(Object o) - Returns true if list contains element


●​ indexOf(Object o) - Index of first occurrence or -1
●​ lastIndexOf(Object o) - Index of last occurrence or -1

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

●​ subList(int fromIndex, int toIndex) - Returns view of portion


et
Java 8+ Enhancements
ne

●​ stream() - Returns a sequential Stream


ap

●​ forEach(Consumer<? super E> action) - Performs action for each element


●​ removeIf(Predicate<? super E> filter) - Removes all elements matching
:J

predicate
●​ sort(Comparator<? super E> c) - Sorts list with provided comparator
In

HashSet (and Set) Methods


ed

Basic Operations
nk

●​ HashSet<E>() - Constructor creates empty set


●​ HashSet<E>(Collection<? extends E> c) - Constructor with collection
Li

●​ size() - Returns number of elements


●​ isEmpty() - Checks if set contains no elements
●​ add(E e) - Adds element if not present (returns boolean)
●​ remove(Object o) - Removes element if present
●​ clear() - Removes all elements
●​ contains(Object o) - Returns true if set contains element

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

●​ iterator() - Returns iterator over elements

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

●​ HashMap<K, V>(Map<? extends K, ? extends V> m) - Constructor with


map
ap

●​ size() - Returns number of key-value mappings


●​ isEmpty() - Checks if map contains no mappings
●​ put(K key, V value) - Associates key with value
:J

●​ get(Object key) - Returns value for key or null


●​ getOrDefault(Object key, V defaultValue) - Returns value or default
In

●​ remove(Object key) - Removes mapping for key


●​ clear() - Removes all mappings
ed

Checking Map Contents


nk

●​ containsKey(Object key) - True if map contains key


Li

●​ containsValue(Object value) - True if map maps to value

Bulk Operations

●​ putAll(Map<? extends K, ? extends V> m) - Copies all mappings


●​ putIfAbsent(K key, V value) - Adds mapping if key not present

Map Views

●​ keySet() - Returns Set view of keys


●​ values() - Returns Collection view of values
●​ entrySet() - Returns Set view of mappings

Java 8+ Enhancements

●​ forEach(BiConsumer<? super K, ? super V> action) - Performs action


for each entry
●​ replaceAll(BiFunction<? super K, ? super V, ? extends V>
function) - Replaces all values
●​ compute(K key, BiFunction<? super K, ? super V, ? extends V>

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

Queue and PriorityQueue Methods


et
Basic Operations
ne

●​ add(E e) / offer(E e) - Adds element to queue (returns boolean)


ap

●​ remove() / poll() - Removes and returns head (poll returns null if empty)
●​ element() / peek() - Retrieves head without removing (peek returns null if empty)
:J

●​ size() - Returns number of elements


●​ isEmpty() - Checks if queue is empty
In

PriorityQueue Specific
ed

●​ PriorityQueue<E>() - Default min heap


●​ PriorityQueue<E>(Comparator<? super E> comparator) - Custom
nk

ordering
Li

Stack Methods
Basic Operations

●​ push(E item) - Pushes item onto top of stack


●​ pop() - Removes and returns top item
●​ peek() - Returns top item without removing
●​ empty() - Tests if stack is empty
●​ search(Object o) - Returns position of object (1-based)
Deque Methods (LinkedList, ArrayDeque)
Basic Operations

●​ addFirst(E e) / offerFirst(E e) - Inserts at front


●​ addLast(E e) / offerLast(E e) - Inserts at end
●​ removeFirst() / pollFirst() - Removes first element
●​ removeLast() / pollLast() - Removes last element
●​ getFirst() / peekFirst() - Retrieves first element
●​ getLast() / peekLast() - Retrieves last element

va
de
ch
Sa
et
ne
ap
:J
In
ed
nk
Li

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy