开发者

Data structure which is good in insertion, removal and random access

Currently, I am looking the following data struct开发者_开发知识库ure.

  1. Fast to insert at tail.
  2. Fast to remove from head.
  3. Able to perform random access.

I realize ArrayBlockingQueue is good at (1) and (2), and ArrayList is good at (3). Is there single data structure from standard library/ Apache libraries/ Google libraries, which enable me to have all 3 requirements at once?


I think the best datastructure for your case is a ringbuffer/circular buffer. The ringbuffer performs all three operations in constant time.

An implementation can be found here and many others here

edit: the problem with ringbuffers is that you should know at the beginning how many elements are in that buffer in worst-case. But there also exist dynamic ringbuffers.


LinkedList may be suitable, if speed is not important for 3. Note that ArrayBlockingQueue is designed for environments where multiple threads will access the List. ArrayList and LinkedList are not. You'd have to wrap them using Collections.synchronizedList() if you need to access them from multiple threads.


LinkedHashMap is the one u need to try.It gives u best of all.

Combination of Hash and Double LinkedList datastructure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜