List Implementations in Java
Master dynamic data structures with simple examples
What are List Implementations?
List implementations in Java provide different ways to store and manage collections of elements. Each implementation has unique characteristics that make it suitable for specific use cases. Understanding these differences helps you choose the right data structure for your program.
ArrayList
Dynamic array, fast random access
Simple meaning
Works like a growable array. You can quickly jump to any element using an index.
Building example
Like a hallway with numbered rooms—you can quickly jump to any room.
Code example
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> rooms = new ArrayList<>();
rooms.add("Room 101");
rooms.add("Room 102");
rooms.add("Room 103");
// Fast access by index
System.out.println("Second room: " + rooms.get(1));
}
}LinkedList
Doubly linked list, fast insert/delete
Simple meaning
Each element points to the next AND previous. Fast when adding or removing items in between.
Building example
Like containers attached in a chain—you can easily add or remove containers anywhere.
Code example
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> containers = new LinkedList<>();
containers.add("Container A");
containers.add("Container B");
containers.add("Container C");
// Fast insertion at the beginning
containers.addFirst("Container X");
System.out.println(containers);
}
}Vector
Synchronized ArrayList
Simple meaning
Works like an ArrayList. But it is thread-safe (good when many people use it at the same time).
Building example
Same hallway of rooms, but with a guard letting one person in at a time.
Code example
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<String> safeRooms = new Vector<>();
safeRooms.add("Safe Room 1");
safeRooms.add("Safe Room 2");
System.out.println(safeRooms);
}
}Stack
Last-In, First-Out (LIFO)
Simple meaning
The last item you put in is the first item you take out. Like a pile of plates.
Building example
A stack of bricks: the last brick added is the first one removed.
Code example
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<String> bricks = new Stack<>();
bricks.push("Brick 1");
bricks.push("Brick 2");
bricks.push("Brick 3");
// Removes the last inserted item
System.out.println("Removed: " + bricks.pop());
}
}Quick Summary Table
| Class | Simple Meaning |
|---|---|
| ArrayList | Dynamic array, fast access |
| LinkedList | Linked nodes, fast insert/delete |
| Vector | Synchronized ArrayList |
| Stack | LIFO structure |
