ArrayDeque vs LinkedList in Java

1. Introduction

Java provides a rich set of built-in collections (data structures), among which ArrayDeque and LinkedList are both popular choices when implementing deques or stacks. However, developers must understand the differences between them to make informed decisions based on the needs of their applications.

2. Key Points

- ArrayDeque and LinkedList are both implementations of the Deque interface.

- ArrayDeque is more efficient for queue operations, while LinkedList has the flexibility to be used as a list or queue.

- ArrayDeque cannot contain null elements; LinkedList can.

- Memory efficiency and performance vary between the two, influencing their application.

3. Differences

ArrayDeque LinkedList
Uses a resizable array as its internal data structure. Uses a doubly-linked list as its internal data structure.
Cannot contain null elements. Can contain null elements.
Generally has better performance for queue operations. Offers better performance for operations at the middle of the list.
More memory-efficient due to a contiguous memory layout. Has additional memory overhead due to the storage of previous and next pointers.

4. Example

 import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Deque;

public class DequeExample {
    public static void main(String[] args) {
        // Creating an ArrayDeque
        Deque<Integer> arrayDeque = new ArrayDeque<>();
        // Creating a LinkedList
        Deque<Integer> linkedList = new LinkedList<>();

        // Step 1: Add elements to the ArrayDeque

        // Step 2: Add elements to the LinkedList

        // Step 3: Remove elements from ArrayDeque
        int dequeuedFromArrayDeque = arrayDeque.removeFirst();

        // Step 4: Remove elements from LinkedList
        int dequeuedFromLinkedList = linkedList.removeFirst();

        // Step 5: Print the results
        System.out.println("Removed from ArrayDeque: " + dequeuedFromArrayDeque);
        System.out.println("Removed from LinkedList: " + dequeuedFromLinkedList);


Removed from ArrayDeque: 20
Removed from LinkedList: 20


1. ArrayDeque and LinkedList are both initialized.

2. The number 10 is added to the end, and the number 20 to the front of both the ArrayDeque and the LinkedList.

3. The first element (which is the number 20) is removed from both the ArrayDeque and the LinkedList.

4. The output shows that the number 20, which was at the front, is successfully removed from both the ArrayDeque and the LinkedList.