Course contentsShow
← Java
Lesson 907 of 2,87021. Generics FundamentalsFree lesson

LinkedList<E> and Other Generic List Implementations

Applying generics to LinkedList and understanding List<E> interface uniformly.

LinkedList<E> and Other Generic List Implementations

What You'll Learn

You'll discover that LinkedList<E> is another generic List<E> implementation that works just like ArrayList<E>, but with different performance characteristics under the hood.

The List<E> Interface: A Uniform Contract

Just as ArrayList<E> provides type-safe, ordered collections, LinkedList<E> does too—because both implement the same List<E> interface. This means the methods you've used with ArrayList (add(), get(), size(), etc.) work identically with LinkedList.

Think of List<E> as a blueprint for "ordered collections." Both ArrayList and LinkedList follow this blueprint, so switching between them requires minimal code changes.

Creating and Using LinkedList<E>

LinkedList<String> names = new LinkedList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");

for (String name : names) {
    System.out.println(name);
}

Notice the syntax is identical to ArrayList<E>. The type parameter <String> ensures type safety at compile time.

Why Multiple Implementations?

ArrayList stores elements in a contiguous array—fast for random access but slower for frequent insertions/deletions.

LinkedList uses nodes connected by references—efficient for adding/removing elements at both ends, but slower for accessing elements by index.

The beauty of generics: you write code against List<E>, then choose the implementation that fits your performance needs.

List<Integer> numbers = new LinkedList<>(); // Can swap with ArrayList
numbers.add(10);
numbers.add(20);

By declaring List<Integer> (the interface), you can later change to new ArrayList<>() without touching the rest of your code.

Key Takeaway: LinkedList<E> and ArrayList<E> are interchangeable implementations of List<E>, offering the same type-safe methods with different internal performance trade-offs—generics let you write flexible code against the interface.