A comprehensive overview of various collection types in Java, including definitions, operations for insertion,
updating, deletion, access, and more, along with their time complexities.
Collection Type Definition
Map (HashMap) Map<K, V> map = new HashMap<>();
ArrayList ArrayList<T> list new ArrayList<>();
Array T[] arr new T[N];
Stack Stack‹T› stack new Stack<›();
Queue (LinkedList) Queue‹T› queue = new LinkedList‹›();
String/StringBuilder Str:Ing str = new Str1ng() ;
HashSet Set<T> set new HashSet<>();
LinkedList LinkedList<T› list = new LinkedList<>();
TreeMap Map<K, V> map new TreeMap<>();
TreeSet Set<T> set new TreeSet<>();
Collection Type Insert/Add fiunction Time Complexity
Map (HashMap) V put(k1, v1); O(1)
ArrayList boolean add(T); 0(1)
Array arr[index] value; O(1)
Stack T push(T); 0(1)
Queue (LinkedList) boolean add(T) j 0(1)
String/StringBuilder sb. append( "text" ) j O(1)
HashSet boolean add(T); O(1)
LinkedList add(T); 0(1)
TreeMap put (K, V) ; O(log n)
TreeSet add(T); O(log n)
Collection Type Update function Time Complexity
Map (HashMap) V put(k1, v1); 0(1)
ArrayList T set(int index, T); 0(1)
Collection Type Update function Time Complexity
Array arr[index] value; O(1)
Stack N/A N/A
Queue (LinkedList) N/A N/A
String/StringBuilder N/A N/A
HashSet N/A N/A
LinkedList set (index, T); 0(n)
TreeMap put(K, V); 0(log n)
TreeSet N/A N/A
Collection Type Delete/Remove function Time Complexity
Map (HashMap) V remove (k1); 0(1)
ArrayList T remove (int index); 0(n)
Array T removed arr[index]; O(1)
Stack T pop(); 0(1)
Queue (LinkedList) T poll( ) ; 0(1)
String/StringBuilder N/A N/A
HashSet boolean remove(T); 0(1)
LinkedList remove (int index) ; 0(n)
TreeMap remove(K); 0(log n)
TreeSet oeuove(T) ; 0(log n)
Collection Type Get/Access function Time Complexity
Map (HashMap) V get (k1); 0(1)
ArrayList T get (int index); 0(1)
Array T value arr[index]; 0(1)
Stack T peel<(); 0(1)
Queue (Linked List) T peeI‹( ) ; 0(1)
String/StringBuilder cha r cha rAt(int 1ndex) ; 0(1)
HashSet N/A N/A
Collection Type Get/Access Function Time Complexity
LinkedList get(int index); 0(n)
TreeMap get(K); O(log n)
TreeSet N/A N/A
Collection Type Size function Time Complexity
Map (HashMap) 1nt size( ) ; 0(1)
ArrayList int size(); 0(1)
Array int length arr.length; O(1)
Stack int size(); O(1)
l]ueue (LinkedList) int size(); 0(1)
String/StringBuilder 1nt length(); O(1)
HashSet int size(); 0(1)
LinkedList 1nt s:tze( ) ; 0(1)
TreeMap int size(); 0(1)
TreeSet int size(); O(1)
Collection Type Check for Empty function Time Complexity
Map (HashMap) boolean isEmpty(); 0(1)
ArrayList boolean isEmpty(); 0(1)
Array N/A N/A
Stack boolean isEmpty(); O(1)
Aueue (LinkedList) boolean isEmpty(); O(1)
String/StringBuilder boolean :tsEmpty(); 0(1)
HashSet boolean isEmpty(); 0(1)
LinkedList boolean isEmpty(); 0(1)
TreeMap boolean isEmpty(); 0(1)
TreeSet boolean isEmpty(); O(1)
Collection Type Contains function Time Complexity
Map (HashMap) boolean containsKey(k1); 0(1)
ArrayList boolean contains(T); 0(n)
Array N/A N/A
Stack N/A N/A
Queue (LinkedList) N/A N/A
String/StringBuilder bool ean conta1ns(Chansequence seq) ; 0(n)
HashSet boolean contains(T); 0(1)
LinkedList contains(T); 0(n)
TreeMap containsKey(K); 0(log n)
TreeSet contains(T); O(log n)
Collection Type Clear All function Time Complexity
Map (HashMap) clean( ) ; 0(n)
ArrayList clear(); 0(n)
Array N/A N/A
Stack clear(); 0(n)
Queue (LinkedList) | clea n( ) ; | 0(n) |
String/StringBuilder | c lea r( ) ; | 0(n)
HashSet | c Mean( ) ; | 0(n)
LinkedList | clea r( ) ; | 0(n)
TreeMap | c Sea r( ) ; | 0(n) |
TreeSet clea r( ) ; 0(n)
Collection Type Iterate Function Time Complexity
Map (HashMap) for (Map.Entry<K, V> entry : map.entrySet()) O(n)
ArrayList for (T item list) 0(n)
Array for (T item arr) 0(n)
Stack for (T item stack) 0(n)
Queue (LinkedList) for (T item queue) 0(n)
String/StringBuilder Ton (cha r ch st n. toCha nArnay( ) ) 0(n)
Collection Type Iterate function Time Complexity
HashSet for (T item set) 0(n)
LinkedList for (T item list) 0(n)
TreeMap for (Map.Entry<K, V> entry map.entrySet()) 0(n)
TreeSet for (T item set) 0(n)
Collection Type Search function Time Complexity
Map (HashMap) boolean containsKey(k1); 0(1)
ArrayList boolean contains(T); O(n)
Array N/A N/A
Stack N/A N/A
Queue (LinkedList) N/A N/A
String/StringBuilder boolean conta1ns(CharSequence seq) ; 0(n)
HashSet boolean contains(T); 0(1)
LinkedList contains(T); 0(n)
TreeMap containsKey(K); O(log n)
TreeSet contains(T); 0(log n)
Collection Type Sort function Time Complexity
Map (HashMap) N/A N/A
ArrayList Collections.sort(list); 0(nlog n)
Array Arrays.sort(arr); 0(n log n)
Stack N/A N/A
Queue (LinkedList) N/A N/A
String/StringBuilder N/A N/A
HashSet N/A N/A
LinkedList N/A N/A
TreeMap N/A N/A
TreeSet N/A N/A
Collection Type Unique/Distinct function Time Complexity
Map (HashMap) Set<K> keyset(); 0(n)
ArrayList Set<T>distinctList newHashSet<>(list); 0(n)
Array N/A N/A
Stack N/A N/A
Queue (LinkedList) N/A N/A
String/StringBuilder N/A N/A
HashSet boolean add(T); O(1)
LinkedList N/A N/A
TreeMap N/A N/A
TreeSet boolean add(T); 0(log n)
Collection Type Reverse fiunction Time Complexity
Map (HashMap) N/A N/A
ArrayList Collections.reverse(list); 0(n)
Array N/A N/A
Stack N/A N/A
Queue (LinkedList) N/A N/A
String/StringBuilder sb. reverse( ) ; 0(n)
HashSet N/A N/A
LinkedList N/A N/A
TreeMap N/A N/A
TreeSet N/A N/A
Time
Collection Type Clone fiunction
Complexity
Map (HashMap) Map<K, V> cloneMap = new HashMap‹>(originalMap); O(n)
ArrayList<T> cloneList new ArrayList<>
ArrayList (originalList); 0(n)
Array N/A N/A
Stack N/A N/A
Queue (LinkedList) N/A N/A
String/StringBuilder N/A N/A
HashSet Set<T> Cloneset =new HashSet<>(originalSet); O(n)
LinkedList N/A N/A N/A
TreeMap N/A
TreeSet N/A N/A
Here are few things I learnt on this platform:
1. Incrementing Occurrence in a Map
To increment the occurrence of a character in a Map , you can use:
Map<Character, Integer> count = new HashMap<>();
for (char current : input.toCharArray()) {
count.put(current, count.getOrDefault(current, 0) + 1);
2. Decrementing Occurrence and Removing Entry
If you want to decrement the occurrence of a character and remove the entry when its value becomes zero:
char charToRemove = 'c';
count.put(charToRemove, count.get(charToRemove) - 1);
count.remove(charToRemove, 0); // Removes entry if the value is 0
3. Change Alphabet Case Without Built-in Functions
• Uppercase to Lowercase: Use bitwise OR with space to convert an uppercase letter to lowercase.
char currentChar = 'A';
char result = (char)(currentChar | ' '); // 'A' becomes 'a'
• Lowercase to Uppercase: Use bitwise AND with underscore _ to convert a lowercase letter to
uppercase.
char currentChar = 'a';
char result = (char)(currentChar & ' '); // 'a' becomes 'A'
Alternatively, you can subtract ASCII values:
char result = (char)(currentChar - 32); // 'a' to 'A'
4. Return Empty Collections
For better readability, return empty collections like this:
return Collections.EMPTY_SET;
return Collections.EMPTY_LIST;
return Collections.EMPTY_MAP;
5. Add Edges Between Nodes in a Graph
You can create adjacency lists for graphs using:
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int[] edge : edgeList) {
graph.computeIfAbsent(edge[0], val -> new ArrayList‹>()).add(edge[1]);
6. Increment and Decrement in a Map (Simpler Approach)
• Incrementing Values:
for (char current : input.toCharArray()) {
count.merge(current, 1, Integer::sum); // Increment by 1
• Decrementing Values:
for (char current : input.toCharArray()) {
count.merge(current, -1, Integer::sum); // Decrement by 1
7. Sorting Arrays and Collections
• Sort a 2D array by the first element of each sub-array:
Arrays.sort(array, (a, b) -> a[0] - b[0]); // Ascending order
• Avoiding Overflow in Sorting:
Arrays.sort(array, (a, b) -> {
if (a[1] == b[1]) return 0;
return a[1] ‹ b[1] ? -1 : 1; // Ascending based on second element
8. Sorting a Map by Keys or Values
You can sort the map by its keys or values using a custom comparator:
Map<Integer, Integer› count = new HashMap‹>();
for (int i : nums) {
count.put(i, count.get0rDefault(i, 0) + 1);
List<Integer> candidates = new ArrayList<>(count.keyset());
Collections.sort(candidates, (w1, W2) -> count.get(W1).equals(count.get(w2)) ?
w1.compareTo(w2) : count.get(w2) - count.get(w1)); // Sort by values
9.Priority Queue for Max Heap
You can create a max-heap using a priority queue:
PriorityQueue‹Integer> heap = new PriorityQueue‹›(Comparator.reverseorder());
10.Converting Array to List
You can convert an array to a list like this:
List<Integer> list = Arrays.asList(1, 2, 3);
11.String Manipulations
• Substring: substring(a, b) means extracting from index a (inclusive) to index b (exclusive).
String sub = str.substring(1, 4); // From index 1 to 3
• Integer to String:
String str = String.valueOf(num);
• String to Integer:
int num = Integer.parseInt(str);
• String to Integer (Binary):
int num = Integer.parseInt(“111“, 2); // Result: 7
• String to Double:
double val = Double.parseDouble(str);
12.Sorting Arrays (Descending Order)
You can use custom comparators to sort arrays in descending order:
Arrays.sort(arr, (a, b) -› b - a); // Sort array in descending order
Simplified Sorting of 2D Arrays
If sorting a 2D array based on multiple columns:
Int[ ][] arr = ((1, 2, 3}, (3, 6, 9});
Arrays.sort(arr, (a, b) -> {
if (a[0] == b[0]) return a[1] - b[1];
return a[0] - b[0];