I'm looking for a Java LIFO structure with unique values. Furthermore it should promote an already inserted value to the front on reinsertion. It would be useful to track the order of focused windows for example.
I know that it isn't really hard to implement by extending or using Stack
or LinkedHashSet
, but maybe I missed an already existing implementation in the standard Java classes.
Not that I'm aware of, but this would do the trick:
class ReinsertionStack<E> extends Stack<E> {
@Override
public E push(E item) {
this.remove(item);
return super.push(item);
}
}
This also guarantees uniqueness so long as you only add to the stack via push()
.