Java
Module 16: Java Collections FrameworkBeginner26 min read

List, ArrayList, LinkedList & Queue/Deque

Master the Java Collections hierarchy, dynamic resizing in ArrayList, doubly-linked nodes in LinkedList, and ArrayDeque/PriorityQueue.

Key Takeaways & Core Concepts
  • `ArrayDeque`: Resizable circular array implementing both Queue (FIFO) and Stack (LIFO). Faster than legacy Stack (which is synchronized) and LinkedList.
  • `PriorityQueue`: Binary min-heap supporting O(log n) offer() / poll() and O(1) peek()—ideal for top-K problems and task scheduling.

Introduction

The Java Collections Framework (JCF) provides standardized, high-performance data structures and algorithms in java.util. Choosing the right collection—understanding its time complexity, memory layout, and ordering guarantees—is one of the highest-leverage skills in Java development and technical interviews.

ArrayList, ArrayDeque & PriorityQueue
1import java.util.*;
2
3public class Main {
4 public static void main(String[] args) {
5 List<String> topics = new ArrayList<>(List.of("Streams", "OOP", "Collections", "JVM"));
6 Collections.sort(topics);
7 System.out.println("Sorted List: " + topics);
8
9 Deque<String> stack = new ArrayDeque<>();
10 stack.push("Page1"); stack.push("Page2");
11 System.out.println("Top of stack: " + stack.pop());
12
13 PriorityQueue<Integer> pq = new PriorityQueue<>(List.of(30, 10, 20));
14 System.out.println("Smallest element: " + pq.poll());
15 }
16}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
16 lines
Note: Programming to interfaces (`List`, `Deque`, `Queue`) lets you swap implementations cleanly.

Collections Hierarchy

Iterable<E> -> Collection<E> -> List<E>, Set<E>, Queue<E> / Deque<E>. (Note: `Map<K, V>` is a separate root interface because it maps keys to values rather than storing standalone elements.)

ArrayList vs LinkedList Deep Dive

FeatureArrayListLinkedList
Internal StructureContiguous Object[] array (grows by 1.5x)Doubly-linked Node<E> objects
Random Access (`get(i)`)O(1)O(n) traversal
Add/Remove at EndO(1) amortizedO(1)
Insert/Remove MiddleO(n) (shifts elements via System.arraycopy)O(1) if node known, O(n) to seek
CPU Cache LocalityExcellent (contiguous memory)Poor (pointer chasing across Heap)

In practice, ArrayList outperforms LinkedList in almost every real-world workload because modern CPUs prefetch contiguous arrays efficiently.

ArrayDeque & PriorityQueue

  • `ArrayDeque`: Resizable circular array implementing both Queue (FIFO) and Stack (LIFO). Faster than legacy Stack (which is synchronized) and LinkedList.
  • `PriorityQueue`: Binary min-heap supporting O(log n) offer() / poll() and O(1) peek()—ideal for top-K problems and task scheduling.

Fail-Fast Iterators

Standard java.util iterators are fail-fast: modifying a list structurally inside a for-each loop throws ConcurrentModificationException. Use iterator.remove() or list.removeIf(...) instead.

Practice & AssessmentJava Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)