Java APIs Flashcards
(15 cards)
1
Q
String
A
"..."
new String(char[])
.charAt(int index)
.length()
.split(String regex)
.toCharArray()
2
Q
StringBuilder
A
.append(char c)
.append(char[] str)
.append(String str)
charAt(int index)
deleteCharAt(int index)
insert(int offset, char/char[]/String)
.length()
.toString()
3
Q
java.util.Arrays
A
.equals(T[] arr1, T[] arr2)
.fill(T[] arr, T value)
.sort(T[] arr)
.asList(E e, ...)
4
Q
Array
A
{value, value, value, ...}
new T[](int size)
arr[int index]
.length
5
Q
Collection to int[]
A
.stream().mapToInt(Integer::intValue).toArray()
6
Q
Map
A
Map<K, V> x = new HashMap<>();
new HashMap<>(int capacity)
.containsKey(K key)
.get(K key)
.getOrDefault(K key, V defaultValue)
.put(K key, V value)
.remove(K key)
.size()
.isEmpty()
-
.entrySet()
->Set<Map.Entry<K,V>>
-
.keySet()
->Set<K>
-
.values()
->Collection<V>
7
Q
Set
A
Set<E> x = new HashSet<>()
.add(E e)
.contains(E e)
.isEmpty()
.remove(E e)
.size()
8
Q
List
A
.add(E e)
.contains(E e)
.get(int index)
.isEmpty()
.remove(int index)
.size()
.toArray()
Collections.sort(List<T> list)
9
Q
Queue
A
Queue<E> queue = new LinkedList<>()
.offer(E e)
.poll()
.peek()
.isEmpty()
.size()
10
Q
Stack
A
Stack<E> stack = new Stack<>()
.push(E e)
.pop()
.peek()
.size()
.empty()
11
Q
Monotonic
A
Stack or queue that is increasing or decreasing in value
If the next item would break the sorting of the collection, remove elements until the next item can be added
12
Q
Formats for Graph Input
A
- Array of edges [x,y] - [[3,4][2,5][2,4][4,3]]
- Adjacency list (outgoing edges from i) - [[1], [2], [0,3]]
- Adjacency matrix (1 means connection) - [[1, 0, 1], [1, 1, 1], [0, 0, 1]]
- Matrix (like a map)
13
Q
Heap
A
- PriorityQueue<T> q = new PriorityQueue<>();</T>
- min heap by default
- .add(T)
- .peek()
- .remove()
- .size()
- maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
14
Q
Binary Search
A
int left = 0; int right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) { return mid; } if (arr[mid] > target) { right = mid - 1; } else { left = mid + 1; } } // target is not in arr, but left is at the insertion point return left;
15
Q
Binary Search with Duplicates
A
int left = 0; int right = arr.length; while (left < right) { int mid = left + (right - left) / 2; if (arr[mid] >= target) { right = mid; } else { left = mid + 1; } } return left;
Finds left most index. To find right most index change line 5 to >