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.
List Interface Examples
1. Creating a List Using ArrayList
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Java");
list.add("API");
System.out.println(list);
}
}
Output
[Java, API]
2. List Allows Duplicate Elements
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Test");
list.add("Test");
System.out.println(list);
}
}
Output
[Test, Test]
3. List Maintains Insertion Order
import java.util.*;
class Demo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(3);
list.add(1);
list.add(2);
System.out.println(list);
}
}
Output
[3, 1, 2]
4. Accessing Elements Using Index
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C");
System.out.println(list.get(1));
}
}
Output
B
5. Modifying Elements Using set()
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = new ArrayList<>(Arrays.asList("Java", "API"));
list.set(1, "Selenium");
System.out.println(list);
}
}
Output
[Java, Selenium]
6. Removing Elements by Index
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
list.remove(1);
System.out.println(list);
}
}
Output
[A, C]
7. Removing Elements by Value
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
list.remove("B");
System.out.println(list);
}
}
Output
[A, C]
8. remove(int) vs remove(Object) (Interview Trap)
import java.util.*;
class Demo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(Arrays.asList(10, 20, 30));
list.remove(1); // removes index 1
// list.remove(Integer.valueOf(20)); // removes value
System.out.println(list);
}
}
Output
[10, 30]
9. Iterating List Using for-each
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = Arrays.asList("X", "Y", "Z");
for (String s : list) {
System.out.println(s);
}
}
}
10. Iterating Using Iterator
import java.util.*;
class Demo {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
11. Iterating Forward & Backward Using ListIterator
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C");
ListIterator<String> it = list.listIterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
while (it.hasPrevious()) {
System.out.print(it.previous() + " ");
}
}
}
Output
A B C C B A
12. Checking Element Existence
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = Arrays.asList("Java", "API");
System.out.println(list.contains("Java"));
}
}
Output
true
13. Getting List Size
import java.util.*;
class Demo {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
System.out.println(list.size());
}
}
Output
4
14. subList() Example
import java.util.*;
class Demo {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(10, 20, 30, 40);
List<Integer> sub = list.subList(1, 3);
System.out.println(sub);
}
}
Output
[20, 30]
15. Arrays.asList() Limitation (Fixed Size)
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B");
// list.add("C"); // ❌ UnsupportedOperationException
System.out.println(list);
}
}
16. Converting Array to List
import java.util.*;
class Demo {
public static void main(String[] args) {
String[] arr = {"Java", "API"};
List<String> list = Arrays.asList(arr);
System.out.println(list);
}
}
17. Converting List to Array
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B");
Object[] arr = list.toArray();
System.out.println(arr.length);
}
}
18. Sorting a List
import java.util.*;
class Demo {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(3, 1, 2);
Collections.sort(list);
System.out.println(list);
}
}
Output
[1, 2, 3]
19. List.of() (Immutable List – Java 9+)
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = List.of("A", "B");
System.out.println(list);
// list.add("C"); // ❌ UnsupportedOperationException
}
}
20. Interview Summary – List Interface
import java.util.*;
class Demo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Ordered");
list.add("Duplicate");
list.add("Duplicate");
System.out.println(list);
}
}
Key Points
- Ordered collection
- Allows duplicates
- Index-based access
- Implementations: ArrayList, LinkedList, Vector
- ListIterator supports bidirectional traversal
Key Takeaway
Use List when order and duplicates matter. Choosing the right implementation (ArrayList vs LinkedList) is critical for performance and scalability.