← Back to Chapters

Java List Interface

? Java List Interface

? Quick Overview

The List interface in Java is part of the java.util package and represents an ordered collection of elements. It allows duplicate values and provides positional access to elements.

? Key Concepts

  • Maintains insertion order
  • Allows duplicate elements
  • Supports index-based access
  • Common implementations: ArrayList LinkedList Vector

? Syntax & Theory

The List interface is implemented by multiple classes. You cannot create an object of List directly, but you can reference it using implementing classes.

? View Code Example
// Declaring a List reference with ArrayList implementation
List names = new ArrayList<>();

? Code Examples

? View Code Example
// Demonstrating basic List operations
import java.util.*;

public class ListDemo {
public static void main(String[] args) {
List numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(10);
System.out.println(numbers);
}
}

?️ Live Output / Explanation

Output

[10, 20, 10]

The List preserves insertion order and allows duplicate elements.

? Interactive Playground

Visualize how ArrayList.add() and remove() work. Notice the indices updates!

List is currently empty []
Current Size: 0

✅ Tips & Best Practices

  • Use ArrayList when fast random access is needed
  • Use LinkedList when frequent insertions/deletions occur
  • Prefer interface reference (List) over class reference

? Try It Yourself

  • Create a List of strings and print each element using a loop
  • Remove an element by index and observe the change
  • Replace an element using the set() method