javaarraylistarraydeque

Can We replace ArrayList with ArrayDeque for better performance?


I was reading Kathy sierra's OCP8 guide and found a line that says:

"ArrayDeque is like an ArrayList with better performance"

Now I am confused about where to use ArrayList and where to use 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. But I want to know which is preferable between the two. Help is much appreciated.


Solution

  • i would suggest use ArrayList over ArrayDeque on below cases

    1. Use an ArrayList if you need to access elements by index and you only need to insert/delete at the end.
    2. Use an ArrayDeque as a stack, queue, or deque.

    Insertion and Deletion in Both Collection.

    ArrayList:

    Worst-case O(n) because you have to shift elements. 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).

    ArrayDeque:

    now answer is in your question. its totally depend on your requirement.after analysing you can easily predict.

    for more please take a look