

Concrete implementations of List interface
ArrayList
is a concrete implementation of the List
interface. It inherits all the properties and methods defined by the List
interface.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.ArrayList
can store objects of different types (heterogeneous objects) as long as they share a common superclass, typically Object
.ArrayList
allows insertion of null values.ArrayList
implements the RandomAccess
interface, which is a marker interface that indicates the list supports fast (generally constant time) random access.ArrayList
provides fast iteration over elements.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);
import java.util.*;
public class ArrayListExample {
public static void main(String[] args) {
// 1. Using default constructor
ArrayList<Object> list = new ArrayList<>();
// 2. Adding heterogeneous objects
list.add("Apple");
list.add(10);
list.add(3.14);
list.add(true);
// 3. Adding null values
list.add(null);
list.add("Banana");
list.add(null);
// 4. Displaying elements in the list
System.out.println("ArrayList elements: " + list);
// Output: ArrayList elements: [Apple, 10, 3.14, true, null, Banana, null]
// 5. Accessing elements using index
System.out.println("Element at index 1: " + list.get(1)); // Output: 10
System.out.println("Element at index 4: " + list.get(4)); // Output: null
// 6. Fast random access
System.out.println("Fast random access to index 2: " + list.get(2)); // Output: 3.14
// 7. Iterating over the list
System.out.println("Iterating over ArrayList:");
for (Object element : list) {
System.out.println(element);
}
// Output:
// Apple
// 10
// 3.14
// true
// null
// Banana
// null
// 8. Using ArrayList(Collection<? extends E> c) constructor
Collection<String> collection = Arrays.asList("X", "Y", "Z");
ArrayList<String> listFromCollection = new ArrayList<>(collection);
System.out.println("ArrayList from collection: " + listFromCollection);
// Output: ArrayList from collection: [X, Y, Z]
// 9. Using ArrayList(int initialCapacity) constructor
ArrayList<String> listWithCapacity = new ArrayList<>(5);
listWithCapacity.add("A");
listWithCapacity.add("B");
System.out.println("ArrayList with initial capacity: " + listWithCapacity);
// Output: ArrayList with initial capacity: [A, B]
}
}
LinkedList
is a concrete implementation of the List
interface. It inherits all the properties and methods defined by the List
interface.LinkedList
is a doubly linked list, which consists of nodes that are linked to both the previous and next nodes.LinkedList
maintains the order of elements as they are inserted.LinkedList
allows duplicate elements.LinkedList
can store objects of different types (heterogeneous objects).LinkedList
allows insertion of null values.LinkedList
implements the Serializable
and Cloneable
interfaces, enabling it to be serialized and cloned.LinkedList
does not implement the RandomAccess
interface. This means accessing elements by index is slower compared to ArrayList
.LinkedList
is the best choice if your frequent operations are insertions and deletions, especially at the beginning or end of the list.LinkedList
is the worst choice if your frequent operation is retrieval or iteration due to its sequential nature.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.
xxxxxxxxxx
import java.util.*;
public class LinkedListExample {
public static void main(String[] args) {
// 1. Using default constructor
LinkedList<Object> list = new LinkedList<>();
// 2. Adding elements to the LinkedList
list.add("Apple");
list.add(10);
list.add(3.14);
list.add(true);
// 3. Adding null values
list.add(null);
list.add("Banana");
list.add(null);
// 4. Displaying elements in the list
System.out.println("LinkedList elements: " + list);
// Output: LinkedList elements: [Apple, 10, 3.14, true, null, Banana, null]
// 5. Accessing elements using getFirst and getLast
System.out.println("First element: " + list.getFirst()); // Output: Apple
System.out.println("Last element: " + list.getLast()); // Output: null
// 6. Inserting elements at the beginning and end
list.addFirst("First");
list.addLast("Last");
System.out.println("LinkedList after adding elements at first and last: " + list);
// Output: LinkedList after adding elements at first and last: [First, Apple, 10, 3.14, true, null, Banana, null, Last]
// 7. Removing elements from the beginning and end
list.removeFirst();
list.removeLast();
System.out.println("LinkedList after removing first and last elements: " + list);
// Output: LinkedList after removing first and last elements: [Apple, 10, 3.14, true, null, Banana, null]
// 8. Using push and pop (stack operations)
list.push("Pushed");
System.out.println("LinkedList after push: " + list);
// Output: LinkedList after push: [Pushed, Apple, 10, 3.14, true, null, Banana, null]
System.out.println("Element popped: " + list.pop()); // Output: Pushed
System.out.println("LinkedList after pop: " + list);
// Output: LinkedList after pop: [Apple, 10, 3.14, true, null, Banana, null]
// 9. Using peek, peekFirst, and peekLast
System.out.println("Element at head (peek): " + list.peek()); // Output: Apple
System.out.println("Element at head (peekFirst): " + list.peekFirst());// Output: Apple
System.out.println("Element at tail (peekLast): " + list.peekLast()); // Output: null
// 10. Using LinkedList(Collection<? extends E> c) constructor
Collection<String> collection = Arrays.asList("X", "Y", "Z");
LinkedList<String> listFromCollection = new LinkedList<>(collection);
System.out.println("LinkedList from collection: " + listFromCollection);
// Output: LinkedList from collection: [X, Y, Z]
}
}
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
Vector
class are synchronized, making it thread-safe.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);
xxxxxxxxxx
import java.util.Vector;
import java.util.Enumeration;
public class VectorSpecificMethodsExample {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
// Adding elements
vector.addElement("Apple");
vector.addElement("Banana");
vector.addElement("Cherry");
// Displaying the first and last elements
System.out.println("First Element: " + vector.firstElement());
System.out.println("Last Element: " + vector.lastElement());
// Inserting an element at a specific position
vector.insertElementAt("Mango", 1);
// Displaying all elements
System.out.println("Elements in vector:");
Enumeration<String> enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
// Ensuring capacity
vector.ensureCapacity(10);
System.out.println("Capacity after ensuring: " + vector.capacity());
// Removing an element
vector.removeElement("Banana");
System.out.println("Elements after removal:");
enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
// Trimming to size
vector.trimToSize();
System.out.println("Capacity after trimming: " + vector.capacity());
}
}
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
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
Vector
.Important Methods
xxxxxxxxxx
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
// Pushing elements onto the stack
stack.push(10);
stack.push(20);
stack.push(30);
// Peeking the top element
System.out.println("Top Element: " + stack.peek());
// Popping elements from the stack
while (!stack.isEmpty()) {
System.out.println("Popped Element: " + stack.pop());
}
}
}
Top Element: 30
Popped Element: 30
Popped Element: 20
Popped Element: 10