List is an ordered collection that:
- allows duplicates,
- maintains insertion order,
- supports index-based access.
It extends the Collection
interface.
Common Implementations
Implementation | Features |
---|---|
ArrayList | Fast reads, resizable array, slow inserts/removes |
LinkedList | Fast inserts/removes, slower random access |
Vector | Like ArrayList but synchronized (legacy) |
Stack | LIFO (extends Vector) |
Example
List<String> names = new ArrayList<>();
names.add("Kamil");
names.add("Kamil"); // duplicates allowed
System.out.println(names.get(0)); // Kamil
Methods
list.add("A");
list.get(0);
list.set(0, "B");
list.remove(0);
list.contains("B");
list.size();
list.clear();
Comparison
ArrayList vs LinkedList
Feature | ArrayList | LinkedList |
---|---|---|
Backed By | Dynamic array | Doubly-linked list |
Access Time | Fast (O(1)) | Slower (O(n) |
Insert/Delete | Slower (Shifts) | Faster (O(1) if at ends) |
Memory | Compact | More (due to nodes) |
Use Cases
- ArrayList: fast access, read-heady apps.
- LinkedList: frequent insert/delete (e.g. queues).
- Vector: thread-safe legacy code (avoid unless needed).
- Stack: LIFO operations (use Deque instead in modern code).
Tip
Be ready to explain:
- compare ArrayList vs LinkedList,
- show time complexity awareness,
- explain why List allows duplicates and index access.
Parent: _Collections