The perfect place for easy learning...

Java Programming

×

Topics List


Java Deque Interface





The Deque interface is a child interface of the Queue interface. The Deque interface is available inside the java.util package. It defines the methods that are used by class ArrayDeque.

🔔 The Deque interface extends Queue interface.

🔔 The Deque interface allows duplicate elements.

🔔 The Deque interface preserves the order of insertion.

The Deque interface defines the following methods.

Deque interface in java

Let's consider an example program on ArrayDeque to illustrate the methods of Deque interface.

Example
import java.util.*;

public class DequeInterfaceExample {

	public static void main(String[] args) {

		Deque deque = new ArrayDeque();
		
		deque.addFirst(10);
		deque.addLast(20);
		deque.offerFirst(5);
		deque.offerLast(25);
		deque.push(2);
		
		System.out.println("\nElements of deque - " + deque);

		System.out.println("\nFirst element - " + deque.getFirst());

		System.out.println("\nLast element - " + deque.getLast());

		System.out.println("\nFirst element - " + deque.peekFirst());

		System.out.println("\nLast element - " + deque.peekLast());

		System.out.println("\nRemove first element - " + deque.pop());

		System.out.println("\nRemove one more first element - " + deque.pollFirst());

		System.out.println("\nRemove last element - " + deque.pollLast());

		System.out.println("\nRemove one more first element - " + deque.removeFirst());

		System.out.println("\nRemove one more last element - " + deque.removeLast());

	}

}

When we run this code, it produce the following output.

Deque interface in java