Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
3. Core APIs & I/O
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

25. LinkedLists: The Alternative

Trading direct memory access for blisteringly fast modifications.

Feb 22, 20264 views0 likes0 fires
18px

[!NOTE] The ArrayList is the default dynamic list for 90% of use cases. But what if you are building an application where you are rapidly inserting or deleting thousands of items from the middle of a list constantly?

If you have an ArrayList of 10,000 items, and you command it to delete the item at index 1... the CPU must painstakingly shift the remaining 9,999 items backwards one by one in memory to fill the gap. This is devastatingly slow.

The LinkedList Architecture

The LinkedList class is an almost identical twin to the ArrayList. They implement the exact same core Interfaces, meaning you interact with them using identical methods (.add(), .get(), .remove()).

However, under the hood, they are wildly different.

A LinkedList does not store data in an unbroken contiguous block of memory. Instead, it stores elements in isolated "Nodes" scattered randomly across RAM.

Each Node contains two things:

  1. The actual data (e.g., "BMW").
  2. A direct memory pointer to the Next node in line, and the Previous node in line.

Why is this good?

If you want to insert a new car directly into the middle of a 10,000 car list, the LinkedList simply tells the node before it to point to the new car, and the new car to point to the node after it. It performs exactly two pointer updates in memory, taking 0.001 milliseconds! It does NOT have to shift 5,000 cars backwards.

Why is this bad?

If you want to read cars.get(5000), an ArrayList can do math and jump directly to that exact spot in memory instantly.

A LinkedList cannot. It has to start at Node 0, ask it where Node 1 is, jump to Node 1, ask it where Node 2 is... and physically traverse the chain 5,000 times until it reaches the target. Thus, Reading data from a LinkedList is extremely slow!

Syntax

The syntax is absolutely identical to an ArrayList.

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList<String> cars = new LinkedList<String>();
    
    // Completely identical to ArrayList!
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.get(0);
    cars.remove(1);
    
    // Bonus methods exclusive to LinkedList!
    cars.addFirst("Mazda");
    cars.addLast("Toyota");
  }
}

[!IMPORTANT] Summary Table

  • Use an ArrayList if you plan to do massive amounts of Random Reading (.get()).
  • Use a LinkedList if you plan to do massive amounts of Inserting/Deleting in the middle or ends of the list.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava
Quiz: LinkedLists
2 questions5 min

Continue Learning

26. HashMaps: Key-Value Architecture

Intermediate
14 min

27. HashSets: The Art of Uniqueness

Intermediate
7 min

28. Iterator: Safe Collection Traversal

Advanced
9 min
Lesson 5 of 10 in 3. Core APIs & I/O
Previous in 3. Core APIs & I/O
24. Enumerable Data Structures
Next in 3. Core APIs & I/O
26. HashMaps: Key-Value Architecture
← Back to Java Programming: From Zero to Enterprise
Back to Java Programming: From Zero to EnterpriseAll Categories