Sai A Sai A
Updated date Jul 12, 2023
In this blog, we will learn how to convert an array to a deque in Java. Explore different methods, including using loops, Arrays.asList(), and Collections.addAll().

Introduction:

In Java, an array is a fundamental data structure that allows you to store a fixed-size sequence of elements. On the other hand, a deque (double-ended queue) is a versatile data structure that allows efficient insertion and removal of elements from both ends. There are various scenarios where converting an array to a deque can be beneficial, such as implementing efficient data manipulation operations. In this blog, we will explore different methods to convert an array to a deque in Java, explaining each method in detail and providing code examples along with their outputs.

Method 1: Using a Loop

One straightforward approach to convert an array to a deque is by using a loop. We can iterate over each element of the array and add it to the deque using the addLast() method. Here's an example code snippet:

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

public class ArrayToDequeExample {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};
        Deque<Integer> deque = new ArrayDeque<>();

        for (Integer element : array) {
            deque.addLast(element);
        }

        System.out.println("Converted deque: " + deque);
    }
}

Output:

Converted deque: [1, 2, 3, 4, 5]

In this method, we create an instance of the ArrayDeque class, which implements the Deque interface. We iterate over each element in the array using a loop and add each element to the deque using the addLast() method. Finally, we print the contents of the deque, which now holds all the elements from the original array.

Method 2: Using Arrays.asList() and ArrayDeque.addAll()

Another approach to convert an array to a deque involves utilizing the Arrays.asList() method and the addAll() method of ArrayDeque. Here's an example:

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayToDequeExample {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};
        Deque<Integer> deque = new ArrayDeque<>();

        deque.addAll(Arrays.asList(array));

        System.out.println("Converted deque: " + deque);
    }
}

Output:

Converted deque: [1, 2, 3, 4, 5]

In this method, we start by creating an empty ArrayDeque instance. Next, we convert the array to a List using the Arrays.asList() method, which returns a fixed-size list backed by the array. We then use the addAll() method of the ArrayDeque to add all the elements from the list to the deque. Finally, we print the contents of the deque, which should match the original array.

Method 3: Using Collections.addAll() and ArrayDeque()

Another alternative to convert an array to a deque is by utilizing the Collections.addAll() method along with an ArrayDeque. Here's an example:

import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;

public class ArrayToDequeExample {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};
        Deque<Integer> deque = new ArrayDeque<>();

        Collections.addAll(deque, array);

        System.out.println("Converted deque: " + deque);
    }
}

Output:

Converted deque: [1, 2, 3, 4, 5]

In this method, we start by creating an empty ArrayDeque. Then, we use the Collections.addAll() method to add all the elements from the array to the deque. The advantage of this approach is that it can be used with any collection type, not just deques. Finally, we print the contents of the deque, which should match the original array.

Conclusion:

In this blog, we explored various methods to convert an array to a deque in Java. We discussed using a loop, the Arrays.asList() method combined with ArrayDeque.addAll(), and the Collections.addAll() method along with ArrayDeque

Comments (0)

There are no comments. Be the first to comment!!!