Java Programming Handbook

    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.

    public interface List<E> extends Collection<E> { // List-specific methods }

    Features of List#

    • Ordered Collection: Maintains the order in which elements are inserted.
    • Allows Duplicates: Unlike Set, a List 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#

    MethodDescription
    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:#

    import java.util.*; public class ArrayListExample { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); System.out.println("ArrayList: " + list); } }

    Output:

    ArrayList: [Apple, Banana, Cherry]

    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:#

    import java.util.*; public class LinkedListExample { public static void main(String[] args) { List<Integer> list = new LinkedList<>(); list.add(10); list.add(20); list.add(30); System.out.println("LinkedList: " + list); } }

    Output:

    LinkedList: [10, 20, 30]

    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:#

    import java.util.*; public class VectorExample { public static void main(String[] args) { List<String> list = new Vector<>(); list.add("Car"); list.add("Bike"); list.add("Bus"); System.out.println("Vector: " + list); } }

    Output:

    Vector: [Car, Bike, Bus]

    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:#

    import java.util.*; public class StackExample { public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); stack.push(1); stack.push(2); stack.push(3); System.out.println("Stack before pop: " + stack); System.out.println("Popped element: " + stack.pop()); System.out.println("Stack after pop: " + stack); } }

    Output:

    Stack before pop: [1, 2, 3] Popped element: 3 Stack after pop: [1, 2]

    Choosing the Right List Implementation#

    FeatureArrayListLinkedListVectorStack
    Internal StructureDynamic ArrayDoubly Linked ListDynamic ArrayExtends Vector (LIFO)
    Search PerformanceFast (O(1))Slow (O(n))Fast (O(1))Slow (O(n))
    Insert/DeleteSlow (O(n))Fast (O(1))Slow (O(n))Fast for LIFO
    Thread SafetyNoNoYesYes

    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

    Last updated on Apr 09, 2025