Do any Java libraries have a random access queue implementation?

I am implementing a sliding window over a stream of events in Java. Therefore, I need a data structure that allows me to do the following:

  • add to the end of the data structure when new events occur;

  • delete from the beginning of the data structure when processing old events;

  • get standard random access ( size(), get(i)) to data structure elements; in general, typical List "read" operations;

  • effective for all of the above operations;

  • is not limited.

No other access is required. And no thread safety is required.

I am currently doing this with an ArrayList to get things going. But I want something more efficient; method remove(0)(2. above) is ineffective at ArrayList.

Numbers 1. and 2. are standard Queue - style operations. However, implementations Queuein the JDK (e.g. ArrayDeque ) do not allow get(i)in 3.

So, I am wondering if there are libraries that have such an implementation and are suitable for commercial use.

If not, I think I will attach to writing my own ...

+5
source share
8 answers

, - , . - . .

+4

, mod, , .

, , .

UPDATE:

, , , , , . , .

+3

, ArrayDeque - :

E get(index){ return elements[(head + index) % size];}
+3

?

, " " .

"", "", .

"" , "".

100, 1000 10000 .

+2

, - ConcurrentSkipListMap, . , pollFirst/LastEntry. , .

+1

, , : LinkedList, , , , .

0

, , , : , get(i) ( ), ArrayDeque.toArray()[i], . toArray() System.arraycopy() , . , - , .

0

O (1) O (log n) delete min; , O (log ** 2 n), . Queue-push , .

rbtree push-push O (log n) , . , contiguos , k- k- .

0

All Articles