[英]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
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)。
ArrayDeque: ArrayDeque:
now answer is in your question.现在答案在你的问题中。 its totally depend on your requirement.after analysing you can easily predict.
它完全取决于您的要求。分析后您可以轻松预测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.