Classes which implements List interface

Concrete implementations of List interface

  • ArrayList
  • LinkedList
  • Vector
  • Stack
#

ArrayList class

ArrayList:

  • Implementation of List Interface: ArrayList is a concrete implementation of the List interface. It inherits all the properties and methods defined by the List interface.
  • Underlying Data Structure: The underlying data structure of ArrayList is a resizable array (also known as a growable array or dynamic array). This allows the ArrayList to dynamically adjust its size when elements are added or removed.
  • Heterogeneous Objects Allowed: ArrayList can store objects of different types (heterogeneous objects) as long as they share a common superclass, typically Object.
  • Null Insertion Possible: ArrayList allows insertion of null values.
  • RandomAccess Interface: ArrayList implements the RandomAccess interface, which is a marker interface that indicates the list supports fast (generally constant time) random access.
  • Performance Characteristics:
    • Fast Iteration: Due to its underlying array structure, ArrayList provides fast iteration over elements.
    • Fast Random Access: Accessing elements by index is very fast (O(1) time complexity).
    • Preferred Over LinkedList: Choose ArrayList over LinkedList when you need fast iteration and random access but don't perform many insertions or deletions in the middle of the list.

Constructors of ArrayList

  • ArrayList(): Constructs an empty list with an initial capacity of ten

    ArrayList<String> list = new ArrayList<>();
    
  • ArrayList(Collection c): Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

    Collection<String> collection = Arrays.asList("A", "B", "C");
    ArrayList<String> list = new ArrayList<>(collection);
    
  • ArrayList(int initialCapacity): Constructs an empty list with the specified initial capacity.

    ArrayList<String> list = new ArrayList<>(20);
    
Code Example

LinkedList class

LinkedList:

  • Implementation of List Interface: LinkedList is a concrete implementation of the List interface. It inherits all the properties and methods defined by the List interface.
  • Underlying Data Structure: The underlying data structure of LinkedList is a doubly linked list, which consists of nodes that are linked to both the previous and next nodes.
  • Insertion Order Preserved: LinkedList maintains the order of elements as they are inserted.
  • Duplicates Allowed: LinkedList allows duplicate elements.
  • Heterogeneous Objects Allowed: LinkedList can store objects of different types (heterogeneous objects).
  • Null Insertion Possible: LinkedList allows insertion of null values.
  • Serializable and Cloneable: LinkedList implements the Serializable and Cloneable interfaces, enabling it to be serialized and cloned.
  • Not RandomAccess: LinkedList does not implement the RandomAccess interface. This means accessing elements by index is slower compared to ArrayList.
  • Performance Characteristics:
    • Efficient Insertion/Deletion: LinkedList is the best choice if your frequent operations are insertions and deletions, especially at the beginning or end of the list.
    • Inefficient Retrieval/Iteration: LinkedList is the worst choice if your frequent operation is retrieval or iteration due to its sequential nature.

Constructors of LinkedList:

  • LinkedList(): Constructs an empty list.

    LinkedList<String> list = new LinkedList<>();
  • LinkedList(Collection c): Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

    Collection<String> collection = Arrays.asList("A", "B", "C");
    LinkedList<String> list = new LinkedList<>(collection);

NOTE:

LinkedList Class: This class implements the Deque interface and thus, by extension, the Queue interface. It provides concrete implementations for all the methods defined in these interfaces and adds some additional methods specific to linked lists.

While LinkedList does not introduce new methods of its own, it does provide concrete implementations for the methods inherited from these interfaces.
#
Code Example

Vector class

The Vector class is a dynamic array that can grow or shrink as needed to accommodate adding and removing items. It implements the List interface and is synchronized, meaning Vector class is thread-safe.

Key Points

  • Synchronized: All methods of the Vector class are synchronized, making it thread-safe.
  • Legacy Class: It is part of the original Java 1.0 version.
  • Implements List: Implements the List interface, so it has all the list operations.

Methods Specific to Vector Class in Java

  • addElement(E obj)

    Adds the specified component to the end of this vector, increasing its size by one.

    Vector<String> vector = new Vector<>();
    vector.addElement("Apple");
    
  • elements()

    Returns an enumeration of the components of this vector.

    Enumeration<String> enumeration = vector.elements();
    while (enumeration.hasMoreElements()) {
        System.out.println(enumeration.nextElement());
    }
    
  • firstElement()

    Returns the first component (the item at index 0) of this vector.

    String first = vector.firstElement();
    System.out.println("First Element: " + first);
  • insertElementAt(E obj, int index)

    Inserts the specified object as a component in this vector at the specified

    vector.insertElementAt("Banana", 1);
  • lastElement()

    Returns the last component of the vector.

    String last = vector.lastElement();
    System.out.println("Last Element: " + last);
    
  • removeAllElements()

    Removes all components from this vector and sets its size to zero.

    vector.removeAllElements();
    
  • removeElement(Object obj)

    Removes the first (lowest-indexed) occurrence of the argument from this vector.

    vector.removeElement("Apple");
    
  • removeElementAt(int index)

    Deletes the component at the specified index.

    vector.removeElementAt(1);
    
  • setElementAt(E obj, int index)

    Sets the component at the specified index of this vector to be the specified object.

    vector.setElementAt("Cherry", 1);
    
Code Example
Output
First Element: Apple
Last Element: Cherry
Elements in vector:
Apple
Mango
Banana
Cherry
Capacity after ensuring: 10
Elements after removal:
Apple
Mango
Cherry
Capacity after trimming: 3

Stack class

The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends the Vector class, which means it inherits all the methods from Vector, and adds additional methods to operate as a stack.

Key Points

  • LIFO: Follows Last-In-First-Out order for adding and removing elements.
  • Extends Vector: Inherits all the properties and methods of Vector.

Important Methods

  1. push(E item): Pushes an item onto the top of this stack.
  2. pop(): Removes the object at the top of this stack and returns that object.
  3. peek(): Looks at the object at the top of this stack without removing it from the stack.
  4. empty(): Tests if this stack is empty.
  5. search(Object o): Returns the 1-based position where an object is on this stack.
Code Example
Output
Top Element: 30
Popped Element: 30
Popped Element: 20
Popped Element: 10