Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 482
Jason S Avatar asked Dec 08 '08 16:12

Jason S


1 Answers

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.

like image 178
bajafresh4life Avatar answered Sep 28 '22 18:09

bajafresh4life