Search code examples
javacollectionsdeque

Java equivalent of std::deque


I'm a relatively new Java programmer coming from C++/STL, and am looking for a class with these characteristics (which the C++ std::deque has, as I understand it):

  1. O(1) performance for insertion/removal at the beginning/end
  2. O(1) performance for lookup by index
  3. are growable collections (don't need fixed size bounds)

Is there a Java equivalent to this? I found the Java 1.6 [ArrayDeque] class which has the insert/removal and growable characteristics, but don't seem to have lookup-by-index unless you call toArray() which would not be O(1).


Solution

  • Primitive Collections for Java has an ArrayDeque with a get(int idx) method.

    http://sourceforge.net/projects/pcj

    I can't vouch for the quality of this project though.

    An alternative would be to get the JDK ArrayDeque source and add the get(int idx) method yourself. Should be relatively easy.

    EDIT: If you intend to use the deque in a highly multi-threaded manner, I would go the "patch the JDK's ArrayDeque" route. This implementation has been tested thoroughly and is used in the new java.util.concurrent ForkJoin framework.