Java Collections Framework- Deque Interface

Java Collections Framework- Deque Interface


Posted in : Java Posted on : November 30, 2010 at 6:02 PM Comments : [ 0 ]

The java.util.Deque interface is a subtype of the java.util.Queue interface.

Java Collections Framework- Deque Interface

The java.util.Deque interface is a subtype of the java.util.Queue interface.Deque is a linear collection that supports element insertion and removal at both ends. Thus, "Deque" is short for "Double Ended Queue" and is pronounced "deck", like a deck of cards. Most Deque implementations place no fixed limits on the number of elements they may contain, but this interface supports capacity-restricted deques as well as those with no fixed
size limit.This interface defines methods to access the elements at both ends of the deque. Methods are provided to insert, remove, and examine the element. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation).

Methods of this interface are as follows :

Queue subtype all methods in the Queue and Collection interfaces are also available in the Deque interface.Deque is an interface you need to instantiate a concrete implementation of the interface in order to use it. for Deque implementations in the Java Collections API we can use:

java.util.ArrayDeque
java.util.LinkedList

LinkedList is a pretty standard deque / queue implementation.ArrayDeque stores its elements internally in an array. If the number of elements exceeds the space in the array, a new array is allocated, and all elements moved over. In other words, the ArrayDeque grows as needed, even if it stores its elements in an array.Here are a few examples of how to create a Deque instance:

  Deque odeque = new LinkedList();

  Deque dequeo = new ArrayDeque();

Here is an example that demonstrates a Deque: DequeDemoExample.java  

package devmanuals.com;

import java.util.*;

public class DequeDemoExample {
	public static void main(String args[]) {
		Deque queue = new ArrayDeque();

		queue.add("A");
		queue.add("B");
		queue.add("C");
		queue.add("D");

		System.out.print("The Deque elements are : " + queue);
	}
}

Output:

The Deque elements are : [A, B, C, D]

 Download This Code

There are some more Examples of these methods :

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics