I'm looking for a collection that:
- is a
Deque
/List
- i.e. supports inserting elements at "the top" (newest items go to the top) -deque.addFirst(..)
/list.add(0, ..)
. It could be aQueue
, but the iteration order should be reverse - i.e. the most recently added items should come first. - is bounded - i.e. has a limit of 20 items
- auto-discards the oldest items (those "at the bottom", added first) when the capacity is reached
- non-blocking - if the deque is empty, retrievals should not block. It should also not block / return false / null / throw exception is the deque is full.
- concurrent - multiple threads should be able to operate on it
I can take LinkedBlockingDeque
and wrap it into my custom collection that, on add
operations checks the size and discards the last item(s). Is there a better option?