Search code examples
javacollections

Java data structure that retains order, does not allow duplicates, and allows removal and insertion at start and end


Is there a Java data structure that:

  • does not allow duplicates
  • retains insertion order
  • allows removal and insertion at either the start or end of the collection

There is LinkedHashSet, but it only allows remove(object), add(object) as per Set.


Solution

  • LinkedHashSet will allow removal of the first element, just do

    Iterator iter = linkedHashSet.iterator();
    if(iter.next()) {
       iter.remove();
    }