Lesson Completion
Back to course

List Interface: Ordered Collections with Index Access

Intermediate
20 minutes4.9Java

1. The Hook (The "Byte-Sized" Intro)

  • In a Nutshell: List is an ordered collection that allows duplicates and provides index-based access (like arrays).
  • Main implementations: ArrayList (fast random access, slow insertion), LinkedList (fast insertion, slow random access), Vector (synchronized ArrayList—legacy). Use ArrayList by default unless you need frequent insertions/deletions.

Think of playlist on Spotify. Songs are in order (indexed), you can have the same song twice (duplicates), and you can jump to track #5 (indexed access). That's a List!


2. Conceptual Clarity (The "Simple" Tier)

💡 The Analogy: The Notebook vs Card Catalog

  • ArrayList: Spiral notebook (pages numbered, easy to flip to page 10)
  • LinkedList: Chain of index cards (must flip through one-by-one)

3. Technical Mastery (The "Deep Dive")

Implementations Comparison

FeatureArrayListLinkedListVector
Data StructureDynamic arrayDoubly-linked listDynamic array
Random AccessO(1)O(n)O(1)
Insert/DeleteO(n)O(1)O(n)
Thread-SafeNoNoYes (synchronized)
Use CaseDefault choiceFrequent insert/deleteLegacy (avoid)

4. Interactive & Applied Code

java
import java.util.*; public class ListDemo { public static void main(String[] args) { // ARRAYLIST: Best for random access List<String> arrayList = new ArrayList<>(); arrayList.add("Apple"); arrayList.add("Banana"); arrayList.add("Apple"); // ✅ Duplicate allowed System.out.println("ArrayList: " + arrayList); System.out.println("Index 1: " + arrayList.get(1)); // O(1) access // LINKEDLIST: Best for frequent insertions List<String> linkedList = new LinkedList<>(); linkedList.add("First"); linkedList.add(0, "New First"); // O(1) at beginning System.out.println("LinkedList: " + linkedList); // List methods arrayList.set(0, "Apricot"); // Replace at index arrayList.remove(1); // Remove by index System.out.println("Modified: " + arrayList); // Iteration for (String fruit : arrayList) { System.out.println(fruit); } // Sorting (in-place) Collections.sort(arrayList); System.out.println("Sorted: " + arrayList); // SubList (view, not copy) List<String> subList = arrayList.subList(0, 2); System.out.println("SubList: " + subList); } }

5. The Comparison & Decision Layer

Decision Tree: ArrayList vs LinkedList

graph TD A{Usage Pattern?} A -- Frequent get by index --> B[ArrayList] A -- Frequent add/remove at ends --> C{Which end?} A -- Frequent iteration --> B A -- Memory constrained --> B C -- Beginning/middle --> D[LinkedList] C -- End only --> B

6. The "Interview Corner" (The Edge)

The "Killer" Interview Question: "Why is ArrayList faster than LinkedList for random access?" Answer: ArrayList stores elements in a contiguous array—accessing index i is simple pointer arithmetic: base_address + (i * element_size) = O(1). LinkedList stores elements as nodes with pointers—you must traverse from head, following pointers: O(n).

Pro-Tip: Initialize capacity if you know size:

java
// ❌ BAD: Default capacity (10), resizes multiple times List<String> list = new ArrayList<>(); for (int i = 0; i < 10000; i++) list.add("item"); // ✅ GOOD: Pre-allocate, no resizing List<String> list = new ArrayList<>(10000); for (int i = 0; i < 10000; i++) list.add("item");

ArrayList resizing is expensive (creates new array, copies all elements)!

Topics Covered

Java FundamentalsData Structures

Tags

#java#collections#list#set#map#arraylist#hashmap

Last Updated

2025-02-01