Iterate over Deque in Java (Forward and Backward directions)

In this post, we will discuss various methods to iterate over Deque in Java.


 

A Deque is a linear collection that supports insertion and removal at both ends. All Deque implementations like ArrayDeque, LinkedList, etc uses “double ended queue” which provides more complete and consistent set of LIFO and FIFO operations.

We should use Deque in preference to Stack class (see this). Below are few functions provided by Deque Interface to support stack operations:

1. push(E e) Pushes the specified element at the head of this deque

2. E pop() Pops an element from the head of this deque. In other words, removes and returns the first element of this deque.

3. Iterator iterator() Returns an iterator over the elements in this deque. The elements are returned from head to tail.

 
Similarly, below are few functions provided by Deque Interface to support queue operations:

1. boolean add(E e) Inserts the specified element at the tail of this deque.

2. E remove() Retrieves and removes the head of this deque (in other words, the first element of this deque).

3. Iterator descendingIterator() Returns an iterator over the elements in this deque in reverse sequential order i.e. the elements are returned from tail to head.

 

1. Using Iterator:

We can use iterator() that returns an iterator to iterate over a deque in LIFO order. i.e. the elements will be traversed from head to tail as shown below:

Java

Download   Run Code

We can use descendingIterator() that returns an iterator to iterate over a deque in FIFO order. i.e. the elements will be traversed from tail to head as shown below:

Java

Download   Run Code

Please note that the iterator will throw a ConcurrentModificationException, if deque is modified after the iterator is created except through the iterator’s own remove method.
 

2. Using enhanced for loop:

As Deque implements Iterable Interface, we can use enhanced for loop to loop through deque in LIFO order as shown below:

Java

Download   Run Code

3. Java 8 – Converting Deque to Streams

In Java 8 and above, we can loop a deque in LIFO order with the help of streams, lambdas and forEach as shown below –

Java

Download   Run Code

4. Converting Deque to Array

We can first convert the deque into an array using toArray() function and then print it using Arrays.toString() function. There are many implementations of toArray() method as shown below:

Java

Download   Run Code

5. Converting Deque to Vector

Enumeration interface provides methods to enumerate through the elements of a Vector. So, we can convert the deque into a vector and finally print all elements of that vector.

Java

Download   Run Code

6. Converting Deque to String

If we’re only required to display contents of the deque, we can print string representation of deque using toString() function as shown below:

Java

Download   Run Code

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz