← All MicroBlogs

Java Collections: List, Map, Queue & Deque

April 28, 2026

javacollectionsfundamentals

πŸ“¦ Java Collections: List, Map, Queue & Deque

Java's collections framework gives you a toolbox of data structures. The trick is knowing which tool fits the job.


1️⃣ ArrayList vs LinkedList

Both implement List β€” same API, very different internals.

List<Person> people = new ArrayList<>();       // backed by a dynamic array
List<String> names  = new LinkedList<>(seed);  // backed by doubly-linked nodes
ArrayListLinkedList
Get by indexO(1) β€” direct jumpO(n) β€” walks the chain
Add to endO(1) amortisedO(1)
Insert/remove middleO(n) β€” shifts elementsO(1) once at the node
MemoryCompact (contiguous)Higher overhead (prev/next pointers per node)

Default to ArrayList. Reach for LinkedList only when you're inserting or removing heavily at the front or middle.


2️⃣ List.of(...) β€” Immutable List

List<String> names = List.of("Alice", "Bob", "Charlie");
names.add("David"); // πŸ’₯ UnsupportedOperationException
  • Fixed size, no nulls, order preserved.
  • Great for constants. If you need to mutate it, wrap it: new ArrayList<>(names) or new LinkedList<>(names).

3️⃣ HashMap

Map<String, Object> map = new HashMap<>();
map.put("john", new Person("John", 30));
map.get("john"); // O(1) average
  • Keyβ†’value lookup/insert/delete all O(1) average.
  • No guaranteed key order β€” use LinkedHashMap (insertion order) or TreeMap (sorted) if you need it.
  • Allows one null key and multiple null values.

Use HashMap any time you need fast lookup by a key rather than by position.


4️⃣ Queue β€” First In, First Out (FIFO)

Think of a checkout line β€” first person in, first served.

Queue<String> queue = new LinkedList<>();
queue.offer("Alice");    // add to tail
queue.offer("Bob");
queue.offer("Charlie");

queue.poll();            // removes & returns "Alice"
queue.peek();            // returns "Bob" without removing
ActionSafe (returns null/false)Throws on failure
Addoffer()add()
Remove headpoll()remove()
Inspect headpeek()element()

Use when: processing tasks in arrival order β€” job queues, BFS graph traversal, event pipelines.


5️⃣ Deque β€” Double-Ended Queue

Pronounced "deck". Works as both a queue and a stack β€” you can add and remove from either end.

Deque<String> deque = new ArrayDeque<>();

// As a queue (FIFO)
deque.offerLast("Alice");
deque.offerLast("Bob");
deque.pollFirst();        // "Alice"

// As a stack (LIFO)
deque.push("Alice");      // pushes to head
deque.push("Bob");
deque.pop();              // "Bob" β€” last in, first out
Front (head)Back (tail)
AddofferFirst() / push()offerLast() / offer()
RemovepollFirst() / pop()pollLast()
InspectpeekFirst()peekLast()

ArrayDeque vs LinkedList as Deque: prefer ArrayDeque β€” no node allocation overhead, faster in practice. Use LinkedList only if you need null elements or the List interface on the same object.


Quick Mental Model

ArrayList   β†’ indexed reads, add to end         β†’ default List choice
LinkedList  β†’ front inserts/removes, or as Deque
List.of()   β†’ immutable constants
HashMap     → fast key→value lookup, no order
Queue       β†’ FIFO line                          β†’ use LinkedList or ArrayDeque
Deque       β†’ both ends open                     β†’ use ArrayDeque

Peace... πŸ€