A fast queue in Java

前端 未结 5 1724
余生分开走
余生分开走 2021-02-04 00:59

I am looking for a fast queue implementation in Java. I see that LinkedList implements the Queue interface, but it will only be as fast as

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 01:31

    If performance of a linked list was really a problem, an alternative would be to implement a "circular queue" in an array, i.e. a queue where the start and end point move as entries are added and deleted. I can give more details if you care. When I was using languages that did not have a library of collections, this was how I always implemented queues because it was easier to write than a linked list and it was faster. But with built-in collections, the effort of writing and debugging my own collection for a special case is not worth the trouble 99% of the time: When it's already written, the fact that I could write it a different way faster than I could re-write it the way Java does is pretty much an irrelevant fact. And any performance gain is likely to be too small to be worth the trouble. I sub-type existing collections to get special behavior I need now and then, but I'm hard-pressed to think of the last time that I wrote one from scratch.

提交回复
热议问题