✅ The
Stack
is the classic Java collection that implements FILO behavior.
Also called LIFO (Last-In-First-Out), meaning:
The first element pushed is the last one popped.
✅ Java Collections with FILO behavior:
Collection | FILO Support | Notes |
---|---|---|
Stack (legacy class) | ✅ Yes | Extends Vector , synchronized (old-style) |
ArrayDeque | ✅ Yes | Modern, fast, recommended over Stack |
LinkedList | ✅ Yes | Can act as both a queue and a stack |
🧪 FILO Example using Stack
:
Stack<String> stack = new Stack<>();
stack.push("A");
stack.push("B");
stack.push("C");
System.out.println(stack.pop()); // C
System.out.println(stack.pop()); // B
System.out.println(stack.pop()); // A
push()
adds to the top, pop()
removes from the top — FILO behavior.
🧪 Modern Alternative: ArrayDeque
as a Stack
Deque<String> stack = new ArrayDeque<>();
stack.push("A");
stack.push("B");
stack.push("C");
System.out.println(stack.pop()); // C
System.out.println(stack.pop()); // B
System.out.println(stack.pop()); // A
✅ Faster and cleaner than Stack
— preferred for most modern Java apps.
🧠 Summary Table
Collection | FILO (Stack) | Recommended? |
---|---|---|
Stack | ✅ Yes | ❌ Legacy class, use Deque instead |
ArrayDeque | ✅ Yes | ✅ Modern & efficient |
LinkedList | ✅ Yes | ✅ Flexible, but slower than ArrayDeque for stack ops |