package jlib; import java.util.HashSet; import java.util.Stack; public class OrderedSet extends HashSet { private Stack order; public OrderedSet() { this.order = new Stack(); } @Override public boolean add(T e) { if (super.add(e)) { this.order.add(e); return true; } return false; } /** * Removes the most recently added element from the set * * @return The element that was removed from the set */ public T pop() { T e = this.order.pop(); this.remove(e); return e; } public T top() { return this.order.peek(); } }