Data Structures
1. Arrays
int[] arr = new int[10];
arr[1] = 34;
arr[1] = 35;
int i = arr[1];
int size = arr.length;
Arrays.fill(arr, -1);
Arrays.sort(arr);
Arrays.sort(arr, (a, b) -> b - a);
int[] range = IntStream.range(0, 10).toArray();
2. Character
char c1 = 'a';
char[] arr = "test".toCharArray();
char c2 = str.charAt(2);
Character.toLowerCase(c)
Character.isLowerCase(c);
Character.toUpperCase(c)
Character.isUpperCase(c);
Character.isLetter(c)
Character.isDigit(c)
Character.isAlphabetic(c);
Character.isLetterOrDigit(c)
3. String & StringBuilder
String str = new String();
str = "abc";
int size = str.length();
char[] arr = str.toCharArray();
String sub = str.substring(1, 3);
str.toLowerCase();
str.toUpperCase();
str.replaceAll("a", "z");
StringBuilder sb = new StringBuilder();
sb.append("hellohowareyou?");
sb.insert(2, "z");
sb.delete(1, 3)
sb.deleteCharAt(0);
sb.reverse();
sb.substring(1);
sb.substring(3, 5);
4. List & ArrayList
List<Integer> list = new ArrayList<>();
list.add(5);
list.addAll(List.of(6, 7, 8, 9, 10));
list.remove(3);
list.set(0, 9);
list.get(0);
int size = list.size();
list.clear();
list.isEmpty();
list.contains(77);
list.indexOf(10);
Integer[] arr = list.toArray(new Integer[0]);
int[] arrPrimitive = list.stream().mapToInt(Integer::intValue).toArray();
Collections.sort(list);
Collections.sort(list, (a, b) -> b - a);
Collections.sort(personList, (p1, p2) -> p2.age - p1.age);
5. Stack
Stack<Integer> st = new Stack<>();
st.push(1);
st.pop();
st.peek();
6. Queue & Deque
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.remove();
q.peek();
q.offer(2);
q.poll()
Deque<Integer> dq = new LinkedList<>();
dq.addFirst(1);
dq.addLast(2);
int f1 = dq.getFirst();
int l1 = dq.getLast();
int f2 = dq.removeFirst();
int l2 = dq.removeLast();
7. PriorityQueue
PriorityQueue<Integer> pq = new PriorityQueue<>();
PriorityQueue<Integer> maxPQ = new PriorityQueue<>(Comparator.reverseOrder());
pq.add(5);
Integer item = pq.remove();
Integer item = pq.peek();
boolean exists = pq.contains(5);
pq.remove(5);
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
PriorityQueue<String> strPQByLength = new PriorityQueue<>((s1, s2) -> s2.length() - s1.length());
PriorityQueue<String> strPQLexi = new PriorityQueue<>();
PriorityQueue<String> strPQLexiDesc = new PriorityQueue<>(Comparator.reverseOrder());
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
PriorityQueue<Person> personPQ = new PriorityQueue<>((p1, p2) -> p2.age - p1.age);
Arrays
1d to 2d and 2d to 1d array
int[][] matrix = new int[m][n];
for(int i=0; i<arr.length; i++) {
int row = i/n;
int col = i%n;
matrix[row][col] = arr[i];
}
int[][] matrix = new int[m][n];
int idx = r*cols + cols;l.07
Concepts
Subarray, substring, subsequence and subsets
| Term | | Use When | Key Method |
| Subset | | Combination of elements, any order | Backtracking |
| Subsequence | 2^n | Keep order, skip allowed | Include/exclude recursion |
| Subarray | | Continuous portion of array | Nested loops (start → end) |
| Substring | | Continuous portion of string | substring(i, j) in loops |