Java ArrayDeque工作原理及实现
https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist/32625029
Another difference to bear in mind: LinkedList supports null elements, whereas ArrayDeque does not
https://stackoverflow.com/questions/34851512/why-null-values-are-not-allowed-in-arraydeque
http://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist
http://stackoverflow.com/questions/6129805/what-is-the-fastest-java-collection-with-the-basic-functionality-of-a-queue
https://zhuanlan.zhihu.com/p/34301705
https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist/32625029
Linked structures are possibly the worst structure to iterate with a cache miss on each element. On top of it they consume way more memory.
If you need add/remove of the both ends, ArrayDeque is significantly better than a linked list. Random access each element is also O(1) for a cyclic queue.
The only better operation of a linked list is removing the current element during iteration.
https://stackoverflow.com/questions/34851512/why-null-values-are-not-allowed-in-arraydeque
From the Javadoc for
Deque
:WhileDeque
implementations are not strictly required to prohibit the insertion of null elements, they are strongly encouraged to do so. Users of anyDeque
implementations that do allow null elements are strongly encouraged not to take advantage of the ability to insert nulls. This is so becausenull
is used as a special return value by various methods to indicated that the deque is empty.
The
ArrayDeque
implementation also uses null
to indicate an empty element (as explained in sestus' and Aman jangra's answers), so the reasoning is two-fold: contract and implementation details.
It's worth noting that it
ArrayDeque
could easily support null elements without much additional complexity, so the more compelling reason seems to be the suggestion of the Deque
interface.
Linked structures are possibly the worst structure to iterate with a cache miss on each element. On top of it they consume way more memory.
If you need add/remove of the both ends, ArrayDeque is significantly better than a linked list. Random access each element is also O(1) for a cyclic queue.
The only better operation of a linked list is removing the current element during iteration.
Another difference to bear in mind: LinkedList supports null elements, whereas ArrayDeque does not.
Also another small disadvantage (for real-time applications) is that on a push/add operation it takes a bit more when the internal array of the ArrayDeque is full, as it has to double its size and copy all the data.
ArrayDeque
is best. See this benchmark, which comes from this blog post about the results of benchmarking this. ArrayDeque
doesn't have the overhead of node allocations that LinkedList
does nor the overhead of shifting the array contents left on remove that ArrayList
has. In the benchmark, it performs about 3x as well as LinkedList
for large queues and even slightly better than ArrayList
for empty queues. For best performance, you'll probably want to give it an initial capacity large enough to hold the number of elements it's likely to hold at a time to avoid many resizes.
Between
ArrayList
and LinkedList
, it seems that it depends on the average number of total elements the queue will contain at any given time and that LinkedList
beats ArrayList
starting at about 10 elements.https://zhuanlan.zhihu.com/p/34301705
ArrayList插入删除一定慢么?
取决于你删除的元素离数组末端有多远,ArrayList拿来作为堆栈来用还是挺合适的,push和pop操作完全不涉及数据移动操作。
取决于你删除的元素离数组末端有多远,ArrayList拿来作为堆栈来用还是挺合适的,push和pop操作完全不涉及数据移动操作。
ArrayList的遍历和LinkedList遍历性能比较如何?
论遍历ArrayList要比LinkedList快得多,ArrayList遍历最大的优势在于内存的连续性,CPU的内部缓存结构会缓存连续的内存片段,可以大幅降低读取内存的性能开销。
ArrayList是如何扩容的?
ArrayList扩容后的大小等于扩容前大小的1.5倍,当ArrayList很大的时候,这样扩容还是挺浪费空间的,甚至会导致内存不足抛出OutOfMemoryError。扩容的时候还需要对数组进行拷贝,这个也挺费时的。所以我们使用的时候要竭力避免扩容,提供一个初始估计容量参数,以免扩容对性能带来较大影响。
数组用来做队列合适么?
队列一般是FIFO的,如果用ArrayList做队列,就需要在数组尾部追加数据,数组头部删除数组,反过来也可以。但是无论如何总会有一个操作会涉及到数组的数据搬迁,这个是比较耗费性能的。
这个回答是错误的!
ArrayList固然不适合做队列,但是数组是非常合适的。比如ArrayBlockingQueue内部实现就是一个环形队列,它是一个定长队列,内部是用一个定长数组来实现的。另外著名的Disruptor开源Library也是用环形数组来实现的超高性能队列,具体原理不做解释,比较复杂。简单点说就是使用两个偏移量来标记数组的读位置和写位置,如果超过长度就折回到数组开头,前提是它们是定长数组。