Java List Interface Explained: Features, Use Cases, and Examples

Illustration for Java List Interface Explained: Features, Use Cases, and Examples
By Last updated:

In Java, the List interface is one of the most widely used components of the Java Collections Framework (JCF). It represents an ordered collection (also known as a sequence), and it allows duplicate elements. Whether you're building a user-facing application or working on backend logic, List offers both flexibility and performance benefits.

In this tutorial, we’ll explore everything you need to know about the List interface—its purpose, internal workings, and real-world applications.


What is the Java List Interface?

Core Definition

  • List is an ordered, index-based collection that can contain duplicate elements.
  • It extends the Collection interface and adds positional access.

Syntax and Class Structure

List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
  • ArrayList: Backed by a dynamically resizing array.
  • LinkedList: Doubly-linked list, better for insert/delete.
  • Vector: Synchronized version of ArrayList (legacy).

Internal Working and Memory Model

ArrayList

  • Backed by an array.
  • Resizes internally (usually 1.5x or 2x) when capacity is exceeded.

LinkedList

  • Each element (Node) holds reference to previous and next.
  • Better for insertion/removal from the middle.

Big-O Performance

Operation ArrayList LinkedList
get(index) O(1) O(n)
add(element) O(1)* O(1)
remove(index) O(n) O(n)
insert(index) O(n) O(1)

* Amortized cost


Real-World Use Cases

  • Maintaining User History: Order matters and duplicates are allowed.
  • UI Elements: Lists of dropdowns, tabs, history stack.
  • Database Results: List of fetched records.

Java 8+ Functional Programming Support

List<String> filtered = names.stream()
    .filter(n -> n.startsWith("A"))
    .collect(Collectors.toList());

Also supports removeIf, forEach, replaceAll methods.


Java Version Differences

📌 What's New in Java Versions?

Java 8

  • Streams API
  • Lambdas with forEach, filter, etc.

Java 9

  • List.of(...) factory methods
List<String> immutableList = List.of("A", "B", "C");

Java 10+

  • var keyword for type inference.

Java 21

  • Improved performance and pattern matching in stream operations.

Best Practices and Misuse

✅ Do

  • Use ArrayList for frequent access by index.
  • Use LinkedList if insert/remove from middle is common.
  • Use List.copyOf() for immutable copies.

❌ Avoid

  • Calling remove() inside for-each loop without iterator.
  • Using LinkedList for random access.
// Wrong: throws ConcurrentModificationException
for (String name : names) {
    names.remove(name);
}

// Correct:
Iterator<String> itr = names.iterator();
while (itr.hasNext()) {
    if (condition) itr.remove();
}

Refactoring Legacy Code

Before:

List names = new ArrayList();
names.add("John");

After:

List<String> names = new ArrayList<>();
names.add("John");

Use generics, forEach loops, and functional APIs to modernize.


Conclusion and Key Takeaways

  • List is ideal when ordering matters and duplicates are needed.
  • Choose ArrayList vs LinkedList based on access vs insert frequency.
  • Prefer List.of() for immutable lists.
  • Use Java 8+ APIs for clean and efficient code.

FAQ

1. Is List ordered in Java?

Yes, it maintains insertion order.

2. Can List contain null values?

Yes, unless it is created using List.of() which throws NPE.

3. How is ArrayList resized internally?

It increases the internal array capacity by 50% (Java 8).

4. When should I prefer LinkedList?

When frequent insertions/deletions from middle are required.

5. What’s the default capacity of ArrayList?

10 in most Java versions.

6. Is ArrayList thread-safe?

No, use Collections.synchronizedList() or CopyOnWriteArrayList.

7. How to remove duplicate entries from List?

Convert to Set and back, or use streams with distinct().

8. Does List allow primitive types?

No. Use wrapper classes like Integer, Double, etc.

9. How does removeIf() work?

It uses a predicate to remove matching elements in a single pass.

10. What happens on List.of(null)?

It throws NullPointerException.