3

I have some doubt regarding this behavior.I assume performance of ArrayList would be same as ArrayDeque while implementing stack as ArrayDeque will use its addLast() method for insertion and removeLast() for deletion.Both will be equally efficient in this case.Please correct me if I am wrong somewhere.

1
  • Why don't you test that behavior? Commented May 1, 2015 at 16:23

2 Answers 2

3

When used as stack, ArrayDeque and ArrayList's performance should be almost the same. Both "push" would be amortized O(1), though their initial capacity might have a slightly affect on the performance.

The default capacity for ArrayList is 10, while the initial capacity of ArrayDeque is 16. Their internal growth policies cause small performance difference which might not be noticable in practice. You can refer to this post for more information.

After all, their "push" operation is amortized O(1).

** This post mentions ArrayDeque's javadoc suggests that it may be slightly faster, but I haven't found the source.

Sign up to request clarification or add additional context in comments.

Comments

0

They are both amorized O(1) with stack operation. The main difference is:

  1. ArrayDeque is resized by factor 2, while ArrayList is 1.5
  2. The initial capacity of ArrayDeque is 16, while ArrayList is 10

So when using them to to store 1024 elements, ArrayDeque calls 6 resize methods, while the ArrayList only calls 12 methods, nealy 2 times of the ArrayDeque.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.