Java – What data structure will hold a bounded stack of items in LIFO

algorithmdata-structuresjavastack

I'm looking for a data structure that is basically a bounded stack.

If I declare that the stack can hold at most 3 items, and I push another item in,
the oldest item is popped.

Best Solution

Well a LIFO (Last In First Out) structure is known as a Stack which is what you need for the main part of your requirement

A FIFO (First In First Out) structure is known as a Queue which is what you need for the ability to pop the oldest Items off the back.

A combination of these is known as a Deque. Where you have to the ability to push or pop from either end.

I'm not sure if Java has a built-in Deque datastructure, but if it does (or you can find an implementation on google), You can just put some wrapping logic around to ensure that if you push to the front, and the deque.Count > 3, then also pop from the back.