← Back to Home

List Interface

The List interface is part of the Java Collections Framework and represents an ordered collection (also called a sequence). It allows duplicate elements and provides index-based access, making it one of the most frequently used collection interfaces in Java. This is a very high-frequency interview topic.

What Is the List Interface?

  • Represents an ordered collection of elements
  • Allows duplicate values
  • Maintains insertion order
  • Supports index-based operations
  • Part of java.util package
List<String> list = new ArrayList<>();
          

Position in Collection Hierarchy

Iterable
   └── Collection
        └── List
             ├── ArrayList
             ├── LinkedList
             └── Vector
          

Key Characteristics of List

  • Preserves order
  • Allows duplicates
  • Allows multiple null values (implementation-dependent)
  • Supports positional access (get(index))

Common Implementations of List

1️⃣ ArrayList

  • Backed by dynamic array
  • Fast random access
  • Slower insertion/deletion in middle
List<Integer> list = new ArrayList<>();
          

2️⃣ LinkedList

  • Backed by doubly linked list
  • Fast insertions/deletions
  • Slower random access
List<Integer> list = new LinkedList<>();
          

3️⃣ Vector (Legacy)

  • Thread-safe (synchronized)
  • Slower due to synchronization
  • Rarely used in modern applications

Important Methods of List Interface

Adding Elements

list.add("Java");
list.add(1, "Python");
          

Accessing Elements

String value = list.get(0);
          

Updating Elements

list.set(0, "C++");
          

Removing Elements

list.remove(1);        // by index
list.remove("Java");  // by value
          

Searching Elements

list.contains("Java");
list.indexOf("Java");
list.lastIndexOf("Java");
          

Iterating a List

Using for-each loop

for (String s : list) {
    System.out.println(s);
}
          

Using Iterator

Iterator<String> it = list.iterator();
while (it.hasNext()) {
    System.out.println(it.next());
}
          

Using ListIterator (Bidirectional)

ListIterator<String> li = list.listIterator();
          

List Allows Duplicate Elements

List<String> list = new ArrayList<>();
list.add("Java");
list.add("Java");
System.out.println(list); // [Java, Java]
          
  • ✔ Duplicates allowed
  • ✔ Order preserved

List vs Set (Quick Comparison)

Aspect List Set
Order Preserved Not guaranteed
Duplicates Allowed Not allowed
Index access Yes No
Common use Sequence data Unique data

When to Use List

  • When order matters
  • When duplicates are allowed
  • When index-based access is required
  • When working with sequential data

Common Beginner Mistakes

  • Using List when uniqueness is required
  • Frequent insertions in ArrayList middle
  • Ignoring generics
  • Concurrent modification during iteration
  • Using Vector unnecessarily

Interview-Ready Answers

Short Answer

The List interface represents an ordered collection that allows duplicate elements.

Detailed Answer

In Java, the List interface extends the Collection interface and represents an ordered collection with positional access. It allows duplicate elements and provides index-based operations. Common implementations include ArrayList and LinkedList.

Key Takeaway

Use List when order and duplicates matter. Choosing the right implementation (ArrayList vs LinkedList) is critical for performance and scalability.