ArrayDeque, LinkedList both can be used as queues and stacks

 

Here are the differences between Stack, ArrayDeque, and LinkedList:

Class inheritance tree

According to the inheritance tree, all three are indirect implementation classes of Collection. ArrayDeque implements the Deque interface, Stack inherits from Vector, and LinkedList implements the Deque and List interfaces

The difference between

Underlying data storage mode

  storage
Stack An array of length 10
ArrayDeque Array of length 16
LinkedList The list

Method reference table

Stack ArrayDeque LinkedList
push(e) addFirst(e)/offerFirst(e) addFirst(e)/offerFirst(e)
pop() removeFirst()/pollFirst() removeFirst()/pollFirst()
peek() getFirst()/peekFirst() getFirst()/peekFirst()

Thread safety

  Thread safety
Stack Thread synchronization
ArrayDeque Thread out of sync
LinkedList Thread out of sync

Performance options

In general, Vector and its subclasses Stack are not recommended

1. Thread synchronization is required

Convert out-of-sync ArrayDeques and LinkedLists into thread synchronization using synchronizedXxx() in the Collections utility class.

2. Frequent insert and delete operations: LinkedList

3. Frequent random access operations: ArrayDeque

4. Unknown initial data volume: LinkedList

It feels like as long as the bottom layer is an array it’s more efficient to access randomly than if the bottom layer is a linked list, but less efficient to insert and delete

Original link: blog.csdn.net/cartoon_/ar…