Understanding List Interface in Java
The List
interface in Java is a part of the Java Collection Framework (JCF) and is used to store ordered collections of elements, allowing duplicate values. Unlike Set
, which does not allow duplicates, List
maintains insertion order and provides positional access to elements.
In this blog, we will explore the List
interface, its key methods, and its implementations: ArrayList
, LinkedList
, Vector
, and Stack
.
What is the List Interface?#
The List
interface is present in the java.util
package and extends the Collection
interface.
Features of List#
- Ordered Collection: Maintains the order in which elements are inserted.
- Allows Duplicates: Unlike
Set
, aList
can store duplicate elements. - Index-Based Access: Provides methods to access elements by index.
- Dynamic Size: Automatically resizes based on the number of elements.
Key Methods of List Interface#
Method | Description |
---|---|
add(E e) | Adds an element to the list. |
add(int index, E element) | Inserts an element at a specific index. |
get(int index) | Retrieves an element at the given index. |
set(int index, E element) | Replaces the element at the given index. |
remove(int index) | Removes the element at the given index. |
indexOf(Object o) | Returns the index of the first occurrence of the element. |
lastIndexOf(Object o) | Returns the index of the last occurrence of the element. |
subList(int fromIndex, int toIndex) | Returns a portion of the list. |
Implementations of List Interface
1. ArrayList (Dynamic Array)#
Characteristics:#
- Uses dynamic array internally.
- Fast for read operations (O(1) for get, O(n) for add/remove in the middle).
- Not synchronized (not thread-safe).
Example:#
Output:
2. LinkedList (Doubly Linked List)#
Characteristics:#
- Uses doubly linked list internally.
- Fast insertions & deletions (O(1) for add/remove at the start/middle, O(n) for get).
- Slightly higher memory usage due to extra references.
Example:#
Output:
3. Vector (Thread-Safe ArrayList)#
Characteristics:#
- Uses dynamic array internally (like
ArrayList
). - Synchronized, making it thread-safe but slower.
- Methods are synchronized to prevent data corruption in multi-threaded environments.
Example:#
Output:
4. Stack (LIFO - Last In, First Out)#
Characteristics:#
- A subclass of
Vector
that follows LIFO (Last-In-First-Out) order. - Provides methods like
push()
,pop()
,peek()
for stack operations.
Example:#
Output:
Choosing the Right List Implementation#
Feature | ArrayList | LinkedList | Vector | Stack |
---|---|---|---|---|
Internal Structure | Dynamic Array | Doubly Linked List | Dynamic Array | Extends Vector (LIFO) |
Search Performance | Fast (O(1)) | Slow (O(n)) | Fast (O(1)) | Slow (O(n)) |
Insert/Delete | Slow (O(n)) | Fast (O(1)) | Slow (O(n)) | Fast for LIFO |
Thread Safety | No | No | Yes | Yes |
Conclusion#
In this blog, we explored the List
interface and its implementations: ArrayList
, LinkedList
, Vector
, and Stack
. Each implementation has its advantages and use cases, so choosing the right one depends on your requirements. In the next blog, we will dive into each implementations