Collections:
HashMap:
-
Definition:
-
insert / update:
V put(k1, v1);
TC: O(1) -
delete:
V remove(k1);
TC: O(1) -
get:
V get(k1);
TC: O(1) -
size:
int size();
TC: O(1) -
check for Empty:
boolean isEmpty();
TC: O(1) -
value present:
boolean containsKey(k1);
TC: O(1) -
remove all map values:
clear();
TC: O(2n + 1): O(n) (n-key, n-value, 1 for map itself) -
Increment an occurrence in map
-
Let’s say you want to decrement the occurrence and when occurrence becomes zero, remove entry.
-
Accessing:
ArrayList:
-
Definition:
-
insert:
boolean add(t)
[TC: O(1)] /add(int index, T)
[TC: O(n)] -
delete:
T remove(int index);
TC: O(n) as you have to shuffle the elements above that point -
set/update index value:
T set(int index, T);
TC: O(1) -
get inde:
T get(int index);
TC: O(1) -
size:
int list.size();
TC: O(1) -
clear elements:
void clear();
TC: O(n) &removeAll
: O(n^2). -
check for Empty:
boolean isEmpty();
TC: O(1) -
value contain check:
boolean contains(t);
TC: O(n) -
get Index of value:
int indexOf(t);
TC: O(n), checking each element one by one -
non primitive to primitive list:
toArray();
TC: O(n) -
Sorting for List:
Stack:
-
Definition:
-
insert:
T push(t);
TC: O(1) -
size:
int size();
TC: O(1) -
look up for head element:
T peek();
TC: O(1) -
remove head element:
T pop();
TC: O(1) -
check for Empty:
boolean isEmpty();
TC: O(1)
Queue:
-
Definition:
-
insert:
boolean add(t);
TC: O(1) -
size:
int size();
TC: O(1) -
look up for head element:
T peek();
TC: O(1) -
remove head element:
T poll();
TC: O(1) -
check for Empty:
boolean isEmpty();
TC: O(1) -
points to remember :
- queue poll vs stack pop
- queue add vs stack push
- we can define queue via LinkedList, PriorityQueue based on use case
String-StringBuilder:
-
Definition: Strings are immutable in JAVA
-
size:
int length();
TC: O(1) -
convert to char Array:
toCharArray();
TC: O(n) -
value for specific index:
charAt(int index);
TC: O(1) -
substring from string:
substring(a,b]
a : inclusive, b: Exclusive, TC: O(n) -
transform to Lowercase:
toLowerCase();
TC: O(n) -
transform to UpperCase:
toUpperCase();
TC: O(n) -
replace all characters in string:
replaceAll(from, to)
TC: O(n) -
Some useful Character properties
- Character.isLetter();
- Character.isAlphabetic();
- Character.isUpperCase();
- Character.isLowerCase();
- Character.isDigit();
Concatenation:
T str1 + str2
StringBuilder:
- new StringBuilder() / new StringBuilder(int)
- append(“adding string”) better way to do
- toString() converting back to string
HashSet:
- Definition:
- insert / update:
boolean add(t);
TC: O(1) - delete:
boolean remove(t);
TC: O(1) - get:
boolean contains(t);
TC: O(1) - size:
int size();
TC: O(1) - check for Empty:
boolean isEmpty();
TC: O(1) - remove all set values:
clear();
TC: O(n)
Heap:
- Definition:
<!-- Max heap that contains pairs - if values for pairs are the same, then they will be sorted ascending (a-z) according to key: --> PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>( (a, b) -> a.getValue().equals(b.getValue()) ? a.getKey().compareTo(b.getKey()) : a.getValue() - b.getValue() );
- add element:
T add(t)
TC: O(log(n)) - view top element:
T peek()
TC: O(1) // Returns but does not remove the top element - remove:
T poll()
TC: O(log(n)) // Returns and removes the top element - size:
int size()
TC: O(1)
Primitives
Array:
-
Definition:
T arr [ ] = new T[N];
N: static size , T : datatype -
insert:
arr[index] = v1;
TC: O(1) -
update:
arr[index] = v2;
TC: O(1) -
get:
T arr[index]
TC: O(1) -
size:
int arr.length
TC: O(1) -
Arrays.fill(arr, 0);
filled array with value=0, TC: O(n) -
Sorting: TC: O(nlogn)
- primitive (int[] ..)
Arrays.sort(arr);
default ascending,
- non-premetive (Integer[] ..)
Arrays.sort(arr);
default ascendingArrays.sort(arr, (a,b): b-a);
descening
- primitive (int[] ..)
2. String:
-
Creation: String gopha = new String(“ok”);
-
String literal: String gopha = “ok”;
-
Size: gopha.length();
-
Accessing:
Extras:
Graph-Edges
Mostly in graph problems we need to add edges between nodes. If node is not created, we first create list for the children and then add edges
Above code can be abstracted as follows :