Attic
DSAExam Prep & Patterns

Master Cheat Sheet

One-page syntax sheet for quick memorization and comprehensive method reference table

A consolidated syntax and method reference across all primary Java collection structures.


One-Page Syntax Sheet (Memorize)

MasterSyntaxSheet.java
// --- 1. ARRAY ---
int[] arr = new int[5];
int len = arr.length;       // field, NOT method
int val = arr[0];

// --- 2. ARRAYLIST ---
List<Integer> list = new ArrayList<>();
list.add(10);               // boolean
list.add(0, 20);            // void
list.get(0);                // E
list.set(0, 99);            // returns previous E
list.remove(0);             // removes at index -> returns E
list.remove(Integer.valueOf(99)); // removes value -> returns boolean
list.size();                // int

// --- 3. QUEUE & DEQUE ---
Queue<Integer> q = new ArrayDeque<>();
q.offer(10);                // boolean (safe)
q.poll();                   // returns E (null if empty)
q.peek();                   // returns E (null if empty)

Deque<Integer> stack = new ArrayDeque<>();
stack.push(10);             // void (inserts at front)
stack.pop();                // returns E (throws if empty)
stack.peek();               // returns E (null if empty)

// --- 4. PRIORITY QUEUE ---
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
minHeap.offer(10);          // O(log n)
minHeap.poll();             // O(log n) -> returns minimum

// --- 5. SET ---
Set<Integer> set = new HashSet<>();
set.add(10);                // boolean (false if duplicate)
set.contains(10);           // boolean

// --- 6. MAP ---
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);            // returns previous V
map.get("A");               // returns V (null if missing)
map.getOrDefault("A", 0);   // safe read
map.merge("A", 1, Integer::sum); // frequency counting
map.keySet();               // Set<K>
map.values();               // Collection<V>
map.entrySet();             // Set<Map.Entry<K, V>>

// --- 7. TREE BOUNDS ---
TreeSet<Integer> ts = new TreeSet<>();
ts.lower(10);               // strictly < 10
ts.floor(10);               // <= 10
ts.ceiling(10);             // >= 10
ts.higher(10);              // strictly > 10

TreeMap<Integer, String> tm = new TreeMap<>();
tm.lowerKey(10);            // strictly < 10
tm.floorKey(10);            // <= 10
tm.ceilingKey(10);          // >= 10
tm.higherKey(10);           // strictly > 10

Method Cheat Sheet Table

ClassMethodInput TypesReturnsComplexityNote
Arrayarr.length—intO(1)Public field
Arrayarr[i]intTO(1)Indexed access
ArrayListadd(e)EbooleanO(1) amortizedAppends at tail
ArrayListadd(i, e)int, EvoidO(n)Shifts elements right
ArrayListget(i)intEO(1)Random indexed read
ArrayListset(i, e)int, EEO(1)Returns old element
ArrayListremove(i)intEO(n)Removes by index
ArrayListremove(o)ObjectbooleanO(n)Removes by .equals()
LinkedListaddFirst(e)EvoidO(1)Prepends to head
LinkedListaddLast(e)EvoidO(1)Appends to tail
LinkedListremoveFirst()—EO(1)Throws if empty
LinkedListpollFirst()—EO(1)Returns null if empty
ArrayDequepush(e)EvoidO(1)Stack push (head)
ArrayDequepop()—EO(1)Stack pop (head)
ArrayDequeoffer(e)EbooleanO(1)Queue enqueue (tail)
ArrayDequepoll()—EO(1)Queue dequeue (head)
PriorityQueueoffer(e)EbooleanO(log n)Sifts up heap
PriorityQueuepoll()—EO(log n)Sifts down replacement root
PriorityQueuepeek()—EO(1)Returns root element
HashSetadd(e)EbooleanO(1) averageReturns false on duplicate
HashSetcontains(o)ObjectbooleanO(1) averageHash bucket lookup
TreeSetceiling(e)EEO(log n)Smallest >= e
TreeSetfloor(e)EEO(log n)Largest <= e
HashMapput(k, v)K, VVO(1) averageReturns previous value
HashMapget(k)ObjectVO(1) averageReturns null if absent
HashMapmerge(k, v, f)K, V, BiFunctionVO(1) averageFrequency counting
TreeMapceilingKey(k)KKO(log n)Smallest key >= k
TreeMapfloorKey(k)KKO(log n)Largest key <= k

Constructors You Should Recognize Instantly

CommonConstructors.java
// 1. ArrayList
List<Integer> a1 = new ArrayList<>();
List<Integer> a2 = new ArrayList<>(50); // Pre-allocates buffer (size is still 0)
List<Integer> a3 = new ArrayList<>(List.of(1, 2, 3)); // Populated mutable copy

// 2. LinkedList
LinkedList<Integer> l1 = new LinkedList<>();
LinkedList<Integer> l2 = new LinkedList<>(List.of(1, 2, 3));

// 3. Set Family
Set<Integer> h1 = new HashSet<>();
Set<Integer> h2 = new HashSet<>(List.of(1, 2, 3));
Set<Integer> lhs = new LinkedHashSet<>(); // Preserves insertion order
NavigableSet<Integer> t1 = new TreeSet<>();
NavigableSet<Integer> t2 = new TreeSet<>(Comparator.reverseOrder()); // Reverse sorted

// 4. Heap / PriorityQueue
PriorityQueue<Integer> pqMin = new PriorityQueue<>(); // Default Min-heap
PriorityQueue<Integer> pqMax = new PriorityQueue<>(Comparator.reverseOrder()); // Max-heap

// 5. Map Family
Map<Integer, String> m1 = new HashMap<>();
Map<Integer, String> m2 = new HashMap<>(Map.of(1, "A", 2, "B"));
NavigableMap<Integer, String> tm1 = new TreeMap<>();
NavigableMap<Integer, String> tm2 = new TreeMap<>(Comparator.reverseOrder());

Performance Selection Guide

RequirementPreferred StructureAlgorithmic Rationale
Fixed-size indexed dataArray (T[])Zero overhead, direct memory offset, no boxing for primitives
General resizable listArrayListFast O(1) random access and amortized append
Frequent end insert/removeArrayDequeCircular buffer, beats LinkedList (no discrete node allocations)
LIFO StackDeque via ArrayDequeEliminates synchronization lock contention of Stack
FIFO QueueQueue via ArrayDequeAmortized O(1) head/tail operations
Priority / ExtremesPriorityQueueO(log n) insertion/removal of root element
Unique, unorderedHashSetAverage O(1) constant time lookup
Unique + insertion orderLinkedHashSetPreserves deterministic insertion order
Unique + sorted / nearestTreeSetO(log n) floor, ceiling, lower, higher, and range sets
Fast key-value lookupHashMapAverage O(1) constant time reads and writes
Key-value + insertion orderLinkedHashMapPredictable iteration; access-order for LRU caches
Sorted keys / rangesTreeMapO(log n) navigation and range slices

On this page