- Map
- ArrayList
- Array[ ]
- Stack
- Queue / PriorityQueue
- String
- Set
- Definition ->Map<K, V> map = new HashMap<>();
- 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)
- Definition -> ArrayList list = new ArrayList<>();
- 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 index-> 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 premitive to premitive list -> toArray(); // TC: O(n)
- Sorting for List ->
Collections.sort(list, (a, b) -> a - b); // ascending , TC: O(nlogn)
Collections.sort(list, (a, b) -> b - a); // descnding , TC: O(nlogn)
- 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)
premitive (int[] ..)
- Arrays.sort(arr); // default ascending,
non-premetive (Integer[] ..)
- Arrays.sort(arr); // default ascending
- Arrays.sort(arr, (a,b) -> b-a); // descening
- Definition ->Stack st = new Stack<>();
- 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)
- Definition -> Queue queue = new LinkedList<>();
- 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
- Definition -> String str = new String();
- 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
- Definition ->Set set = new HashSet<>();
- 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)
Java Collections Overview Videos
The first 2 minutes of each video go over each concept, and the remaining ~10 minutes shows examples of the available methods.
