简体   繁体   English

我们可以用 ArrayDeque 替换 ArrayList 以获得更好的性能吗?

[英]Can We replace ArrayList with ArrayDeque for better performance?

I was reading Kathy sierra's OCP8 guide and found a line that says:我正在阅读 Kathy sierra 的 OCP8 指南,发现有一行是这样写的:

"ArrayDeque is like an ArrayList with better performance" “ArrayDeque 就像一个性能更好的 ArrayList”

Now I am confused about where to use ArrayList and where to use ArrayDeque .现在我对在哪里使用ArrayList和在哪里使用ArrayDeque感到困惑。 I also know that ArrayDeque is always resized to a power of 2. On resize, the capacity is doubled, so this might be a performance hit in some cases.我也知道 ArrayDeque 总是调整为 2 的幂。调整大小时,容量会增加一倍,因此在某些情况下这可能会影响性能。 But I want to know which is preferable between the two.但我想知道两者之间哪个更可取。 Help is much appreciated.非常感谢帮助。

i would suggest use ArrayList over ArrayDeque on below cases我建议在以下情况下使用 ArrayList 而不是 ArrayDeque

  1. Use an ArrayList if you need to access elements by index and you only need to insert/delete at the end.如果您需要按索引访问元素并且只需要在最后插入/删除,请使用 ArrayList。
  2. Use an ArrayDeque as a stack, queue, or deque.将 ArrayDeque 用作堆栈、队列或双端队列。

Insertion and Deletion in Both Collection.在两个集合中插入和删除。

ArrayList:数组列表:

Worst-case O(n) because you have to shift elements.最坏情况为 O(n),因为您必须移动元素。 Insertion/deletion at the end is faster because there are fewer elements to shift.最后的插入/删除速度更快,因为要移动的元素更少。 If you insert when the Arraylist is full, you have to copy the elements into a new larger array, which is O(n).如果在 Arraylist 已满时插入,则必须将元素复制到新的更大的数组中,即 O(n)。

  • Insertion at the end of an ArrayList takes amortized constant time.在 ArrayList 末尾插入需要分摊常数时间。 This means that a sequence of n insertions into an initially empty ArrayList has a worst-case runtime of O(n), so the average runtime per insertion is O(1), although some insertions may be slower.这意味着在最初为空的 ArrayList 中的 n 个插入序列的最坏情况运行时间为 O(n),因此每次插入的平均运行时间为 O(1),尽管某些插入可能会更慢。 This is achieved by always increasing the array size by a constant factor, because the total number of elements copied is the sum of a geometric series.这是通过始终将数组大小增加一个常数因子来实现的,因为复制的元素总数是几何级数的总和。

ArrayDeque: ArrayDeque:

  • Deletion at the front or back is O(1), and insertion at the front or back takes amortized constant time.在前面或后面删除是O(1),在前面或后面插入需要分摊常数时间。 The JCF implementation does not allow insertion/deletion by index (if it was allowed, it would be worst-case O(n) because of shifting). JCF 实现不允许按索引插入/删除(如果允许,由于移位,最坏的情况是 O(n))。
  • Array deques have no capacity restrictions and they grow as necessary to support usage.数组双端队列没有容量限制,它们会根据需要增长以支持使用。
  • They are not thread-safe which means that in the absence of external synchronization它们不是线程安全的,这意味着在没有外部同步的情况下
  • Null elements are prohibited in the ArrayDeque. ArrayDeque 中禁止使用空元素。

now answer is in your question.现在答案在你的问题中。 its totally depend on your requirement.after analysing you can easily predict.它完全取决于您的要求。分析后您可以轻松预测。

for more please take alook更多请看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM