Java.Collections.Which collection implements the FILO maintenance discipline?

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

CollectionFILO SupportNotes
Stack (legacy class)✅ YesExtends Vector, synchronized (old-style)
ArrayDeque✅ YesModern, fast, recommended over Stack
LinkedList✅ YesCan 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

CollectionFILO (Stack)Recommended?
Stack✅ Yes❌ Legacy class, use Deque instead
ArrayDeque✅ Yes✅ Modern & efficient
LinkedList✅ Yes✅ Flexible, but slower than ArrayDeque for stack ops
This entry was posted in Без рубрики. Bookmark the permalink.