3

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.

gprathour
  • 14,813
  • 5
  • 66
  • 90
raffael
  • 2,427
  • 4
  • 26
  • 42
  • An ordered set might be better than a stack for this, as it has unique values and is still sorted. Not sure how hard would it be to move a again-inserted element on start (probably not much). – kajacx Jul 08 '14 at 11:29

2 Answers2

2

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().

Jamie Cockburn
  • 7,379
  • 1
  • 24
  • 37
0

I think what you want is the Stack class http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Stack.html

The methods that might interest you :

pop() Removes the object at the top of this stack and returns that object as the value of this function.

E push(E item) Pushes an item onto the top of this stack.

Hope that help.

The search method, can give you an easy way to deal with already inserted object

Heaven42
  • 329
  • 1
  • 13