public class LatestElementQueue<E> { public LatestElementQueue() {} private LinkedList<E> list = new LinkedList<E>(); ReentrantLock lock = new ReentrantLock(); public void add(E o) { list.addFirst(o); } public void addAll(List<E> es) { for (E e : es) { list.add(e); } } public void addAndRemOld(E o) { lock.lock(); try { list.remove(o); list.add(o); } finally { lock.unlock(); } } public E poll() { return list.removeLast(); } public int size() { return list.size(); } public synchronized List<E> getTemp() { List<E> es = new ArrayList<E>(); for (E e : list) { es.add(e); } return es; } }