ArrayList
ArrayList
2 LinkedList 3
3 Stack 6
4 Queue 8
5 String 9
6 Map 13
7 Set 19
8 Array 24
ArrayList<String> list = new ArrayList<>();
1. Adding Elements
add(E e): Appends the specified element to the end of the list.
list.add("Element");
add(int index, E element): Inserts the specified element at the specified position.
list.add(1, "Element");
2. Accessing Elements
get(int index): Returns the element at the specified position.
String element = list.get(0);
3. Modifying Elements
set(int index, E element): Replaces the element at the specified position with the specified
element.
list.set(1, "NewElement");
4. Removing Elements
remove(int index): Removes the element at the specified position.
list.remove(0);
remove(Object o): Removes the first occurrence of the specified element.
list.remove("Element");
clear(): Removes all elements from the list.
list.clear();
5. Checking Size
size(): Returns the number of elements in the list.
int size = list.size();
8. Converting to Array
toArray(): Converts the list to an array.
Object[] array = list.toArray();
toArray(T[] a): Converts the list to an array of the specified type.
String[] array = list.toArray(new String[0]);
2. Removing Elements
remove(): Removes the first element from the list.
list.remove();
remove(int index): Removes the element at the specified position.
list.remove(1);
remove(Object o): Removes the first occurrence of the specified element.
list.remove("Element");
removeFirst(): Removes the first element (throws an exception if the list is empty).
list.removeFirst();
removeLast(): Removes the last element (throws an exception if the list is empty).
list.removeLast();
3. Accessing Elements
get(int index): Returns the element at the specified position.
String element = list.get(0);
getFirst(): Returns the first element (throws an exception if the list is empty).
String first = list.getFirst();
getLast(): Returns the last element (throws an exception if the list is empty).
String last = list.getLast();
4. Modifying Elements
set(int index, E element): Replaces the element at the specified position with the specified
element.
list.set(1, "NewElement");
5. Checking Size
size(): Returns the number of elements in the list.
int size = list.size();
9. Converting to Array
toArray(): Converts the list to an array.
Object[] array = list.toArray();
1. Adding Elements
stack.push("Element");
2. Removing Elements
pop(): Removes and returns the top element of the stack. Throws EmptyStackException if the
stack is empty.
3. Accessing Elements
peek(): Returns the top element of the stack without removing it. Throws
EmptyStackException if the stack is empty.
4. Checking Size
search(Object o): Returns the 1-based position of the element in the stack, or -1 if the
element is not found.
int position = stack.search("Element");
6. Checking If Empty
forEach(Consumer<? super E> action): Performs the specified action for each element.
stack.forEach(System.out::println);
8. Converting to Array
Collections.reverse(stack);
Collections.sort(stack);
2. Accessing Elements
peek(): Retrieves the head of the queue without removing it. Returns null
if the queue is empty.
String head = queue.peek();
element(): Retrieves the head of the queue without removing it. Throws an
exception if the queue is empty.
String head = queue.element();
3. Removing Elements
remove(): Removes and returns the head of the queue. Throws an exception
if the queue is empty.
String head = queue.remove();
poll(): Removes and returns the head of the queue. Returns null if the
queue is empty.
String head = queue.poll();
4. Checking Size
size(): Returns the number of elements in the queue.
int size = queue.size();
5. Checking If Empty
isEmpty(): Returns true if the queue is empty, otherwise false.
boolean empty = queue.isEmpty();
7. Converting to Array
toArray(): Converts the queue to an array.
Object[] array = queue.toArray();
STRING
1. Basic Methods
1. char charAt(int index)
2. int length()
3. boolean isEmpty()
2. Comparison Methods
System.out.println(s.lastIndexOf('a')); // 5
System.out.println(s.lastIndexOf("na")); // 4
6. String Modification
String s = "apple,banana,orange";
String[] arr = s.split(",");
System.out.println(arr[1]); // banana
40-41. join()
8. Formatting Methods
42-43. format()
String s = "Java123";
System.out.println(s.matches("\\w+")); // true
String s = "Hello";
System.out.println(s.intern() == s); // true
s.chars().forEach(System.out::println);
System.out.println(String.valueOf(100)); // "100"
System.out.println(String.valueOf(true)); // "true"
System.out.println(String.valueOf(3.14)); // "3.14"
MAP
1. Basic Methods
3. V get(Object key)
5. V remove(Object key)
9. boolean isEmpty()
4. Advanced Methods
5. Bulk Operations
In Java, a Map (e.g., HashMap, TreeMap, LinkedHashMap) stores key-value pairs, and we often need to
iterate over all entries to access keys, values, or both.
This is the most efficient way when you need both keys and values.
It avoids multiple lookups (get() calls) for values, making it more efficient.
If you only need keys, you can iterate over keySet() and fetch values manually.
map.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey()) // Sorts by key
.forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " +
entry.getValue()));
Performance Comparison
Method Time Complexity Use Case
entrySet() (Best) O(n) Both keys & values
keySet() + get() O(n) (but extra lookup) If only keys are needed
values() O(n) If only values are needed
forEach() (Lambda) O(n) Concise Java 8+ solution
Streams O(n log n) (with sorting) Advanced filtering/sorting
Conclusion
✅ Use entrySet() for best efficiency when keys and values are required.
✅ Use forEach() or streams for modern, clean code.
🚫 Avoid keySet() if you also need values, as it requires extra lookups.
SET
1. Creating a Set
import java.util.*;
1. boolean add(E e)
2. boolean remove(Object o)
set.remove("Apple");
System.out.println(set); // [Banana]
4. boolean removeAll(Collection<?> c)
set.removeAll(Arrays.asList("Cherry", "Dates"));
System.out.println(set); // [Banana]
5. boolean retainAll(Collection<?> c)
set.add("Apple");
set.add("Cherry");
set.retainAll(Arrays.asList("Apple"));
System.out.println(set); // [Apple]
6. void clear()
set.clear();
System.out.println(set.isEmpty()); // true
3. Checking Elements
7. boolean contains(Object o)
Checks if an element exists.
8. boolean containsAll(Collection<?> c)
9. Iterator<E> iterator()
Iterator<Integer> it = numSet.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// 1
// 2
// 3
numSet.forEach(System.out::println);
System.out.println(numSet.size()); // 3
System.out.println(numSet.isEmpty()); // false
16. E first()
17. E last()
System.out.println(((TreeSet<Integer>) treeSet).last()); // 30
18. E lower(E e)
System.out.println(((TreeSet<Integer>) treeSet).lower(25)); // 20
19. E higher(E e)
System.out.println(((TreeSet<Integer>) treeSet).higher(25)); // 30
20. E ceiling(E e)
Finds the smallest element greater than or equal to the given value.
System.out.println(((TreeSet<Integer>) treeSet).ceiling(20)); // 20
21. E floor(E e)
Finds the largest element less than or equal to the given value.
System.out.println(((TreeSet<Integer>) treeSet).floor(25)); // 20
22. E pollFirst()
System.out.println(((TreeSet<Integer>) treeSet).pollFirst()); // 10
System.out.println(treeSet); // [20, 30]
23. E pollLast()
Retrieves and removes the last (largest) element.
System.out.println(((TreeSet<Integer>) treeSet).pollLast()); // 30
System.out.println(treeSet); // [20]
# Method Description
1 add(E e) Adds an element
2 remove(Object o) Removes an element
3 addAll(Collection<? extends E> c) Adds all elements
4 removeAll(Collection<?> c) Removes multiple elements
5 retainAll(Collection<?> c) Retains common elements
6 clear() Clears the set
7 contains(Object o) Checks for an element
8 containsAll(Collection<?> c) Checks if all elements exist
9 iterator() Returns an iterator
10 forEach(Consumer<? super E> action) Iterates over elements
11 spliterator() Returns a Spliterator
12 size() Returns size
13 isEmpty() Checks if empty
14 toArray() Converts to an array
15 toArray(T[] a) Converts to a typed array
16 first() Returns first element (TreeSet)
17 last() Returns last element (TreeSet)
18 lower(E e) Element less than given value
19 higher(E e) Element greater than given value
20 ceiling(E e) Smallest ≥ given value
21 floor(E e) Largest ≤ given value
22 pollFirst() Removes first element
23 pollLast() Removes last element
Array
1. Declaring and Initializing an Array
// Declaring an array
int[] arr1 = new int[5]; // Default values: [0, 0, 0, 0, 0]
int[] arr2 = {1, 2, 3, 4, 5}; // Direct initialization
String[] names = new String[]{"Alice", "Bob", "Charlie"}; // String array
3. Length of an Array
System.out.println(arr2.length); // Output: 5
4. Iterating Through an Array
import java.util.Arrays;
System.out.println(Arrays.toString(arr2)); // Output: [1, 2, 3, 4, 5]
6. Multi-Dimensional Arrays
int[][] matrix = new int[2][3]; // 2 rows, 3 columns
int[][] numbers = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(numbers[1][2]); // Output: 6
Iterating a 2D Array
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
/*
Output:
1 2 3
4 5 6
*/
Method Description
toString() Converts array to a string
sort() Sorts an array
parallelSort() Sorts using parallel processing
fill() Fills an array with a value
copyOf() Copies an array
copyOfRange() Copies a subrange of an array
equals() Compares two arrays
binarySearch() Searches in a sorted array
asList() Converts array to a list
deepToString() Prints multi-dimensional arrays
stream() Converts array to stream
Method Description
sum() Computes the sum of array elements
filter() Filters elements using a condition